diff --git a/.gitignore b/.gitignore index a813de4..eb161bb 100644 --- a/.gitignore +++ b/.gitignore @@ -77,6 +77,11 @@ target/ */target/ # Jupyter Notebook .ipynb_checkpoints +*/.ipynb_checkpoints/* + +# ossutil +.ossutil_checkpoint +*/.ossutil_checkpoint/* # pyenv .python-version diff --git "a/notebook/LeetCode/\351\235\242\350\257\225\351\242\23064.\346\261\2021+2+\342\200\246+n.ipynb" "b/notebook/LeetCode/\351\235\242\350\257\225\351\242\23064.\346\261\2021+2+\342\200\246+n.ipynb" new file mode 100644 index 0000000..609bf3c --- /dev/null +++ "b/notebook/LeetCode/\351\235\242\350\257\225\351\242\23064.\346\261\2021+2+\342\200\246+n.ipynb" @@ -0,0 +1,138 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# https://leetcode-cn.com/problems/qiu-12n-lcof\n", + "class Solution:\n", + " def __init__(self):\n", + " self.res = 0\n", + " def sumNums(self, n: int) -> int:\n", + " n > 1 and self.sumNums(n - 1)\n", + " self.res += n\n", + " return self.res\n", + "\n", + "s = Solution()\n", + "s.sumNums(3)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "45" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = Solution()\n", + "s.sumNums(9)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class Solution:\n", + " def __init__(self):\n", + " self.res = 0\n", + " def sumNums(self, n: int) -> int:\n", + " n > 1 and self.sumNums(n - 1)\n", + " self.res += n\n", + " return self.res" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "12" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "6 << 1" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "6 >> 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/notebook/aliyun/oss/sts/signature.ipynb b/notebook/aliyun/oss/sts/signature.ipynb new file mode 100644 index 0000000..d4b5546 --- /dev/null +++ b/notebook/aliyun/oss/sts/signature.ipynb @@ -0,0 +1,160 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 90, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('AccessKeyId', 'testid'),\n", + " ('Action', 'AssumeRole'),\n", + " ('Format', 'JSON'),\n", + " ('RoleArn', 'acs:ram::1234567890123:role/firstrole'),\n", + " ('RoleSessionName', 'client'),\n", + " ('SignatureMethod', 'HMAC-SHA1'),\n", + " ('SignatureNonce', '571f8fb8-506e-11e5-8e12-b8e8563dc8d2'),\n", + " ('SignatureVersion', '1.0'),\n", + " ('Timestamp', '2015-09-01T05:57:34Z'),\n", + " ('Version', '2015-04-01')]" + ] + }, + "execution_count": 90, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Python Version: 3.7\n", + "# 计算签名\n", + "# https://help.aliyun.com/document_detail/28761.html?spm=a2c4g.11186623.6.791.66725328JBIR5G\n", + "\n", + "\n", + "#https://sts.aliyuncs.com/?&AccessKeyId=testid&SignatureMethod=HMAC-SHA1&Version=2015-04-01&Action=AssumeRole&SignatureNonce=571f8fb8-506e-11e5-8e12-b8e8563dc8d2\n", + "params = {\n", + " \"Action\": \"AssumeRole\",\n", + " \"Format\": \"JSON\",\n", + " \"Version\": \"2015-04-01\",\n", + " \"SignatureMethod\": \"HMAC-SHA1\",\n", + " \"SignatureNonce\": \"571f8fb8-506e-11e5-8e12-b8e8563dc8d2\",\n", + " \"SignatureVersion\": \"1.0\",\n", + " \"AccessKeyId\": \"testid\",\n", + " \"Timestamp\": \"2015-09-01T05:57:34Z\",\n", + " \"RoleArn\": \"acs:ram::1234567890123:role/firstrole\",\n", + " \"RoleSessionName\": \"client\"\n", + "}\n", + "\n", + "# 按照首字母排序\n", + "params_arr = list(params.items())\n", + "params_arr.sort(key=lambda x: x[0])\n", + "params_arr\n" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'GET&%2F&AccessKeyId%3Dtestid%26Action%3DAssumeRole%26Format%3DJSON%26RoleArn%3Dacs%253Aram%253A%253A1234567890123%253Arole%252Ffirstrole%26RoleSessionName%3Dclient%26SignatureMethod%3DHMAC-SHA1%26SignatureNonce%3D571f8fb8-506e-11e5-8e12-b8e8563dc8d2%26SignatureVersion%3D1.0%26Timestamp%3D2015-09-01T05%253A57%253A34Z%26Version%3D2015-04-01'" + ] + }, + "execution_count": 91, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from collections import OrderedDict\n", + "import urllib.parse\n", + "\n", + "# 拼接 sign_str\n", + "sign_str = \"GET&%2F&\"\n", + "sign_dict = OrderedDict(params_arr)\n", + "params_str = urllib.parse.urlencode(sign_dict)\n", + "params_str = urllib.parse.quote(params_str)\n", + "\n", + "sign_str += params_str\n", + "sign_str" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "metadata": {}, + "outputs": [], + "source": [ + "# 验证签名用的 str 是否正确\n", + "eg = \"GET&%2F&AccessKeyId%3Dtestid%26Action%3DAssumeRole%26Format%3DJSON%26RoleArn%3Dacs%253Aram%253A%253A1234567890123%253Arole%252Ffirstrole%26RoleSessionName%3Dclient%26SignatureMethod%3DHMAC-SHA1%26SignatureNonce%3D571f8fb8-506e-11e5-8e12-b8e8563dc8d2%26SignatureVersion%3D1.0%26Timestamp%3D2015-09-01T05%253A57%253A34Z%26Version%3D2015-04-01\"\n", + "assert sign_str == eg" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'gNI7b0AyKZHxDgjBGPDgJ1Ce3L4='" + ] + }, + "execution_count": 82, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import hashlib\n", + "import hmac\n", + "import base64\n", + "h = hmac.new('testsecret&'.encode('utf-8'), sign_str.encode('utf-8'), hashlib.sha1)\n", + "sign = h.digest()\n", + "sign = base64.b64encode(sign).decode()\n", + "sign" + ] + }, + { + "cell_type": "code", + "execution_count": 89, + "metadata": {}, + "outputs": [], + "source": [ + "# 验证签名值\n", + "assert sign == 'gNI7b0AyKZHxDgjBGPDgJ1Ce3L4='" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/notebook/config.py b/notebook/config.py new file mode 100644 index 0000000..b47accf --- /dev/null +++ b/notebook/config.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python +# -*- coding:utf-8 -*- +# Author: wxnacy(wxnacy@gmail.com) +# Description: + +DATABASE_URL = 'mysql://root:wxnacy@127.0.0.1:3306/study?charset=utf8mb4' \ No newline at end of file diff --git a/notebook/convert.sh b/notebook/convert.sh new file mode 100755 index 0000000..ff06820 --- /dev/null +++ b/notebook/convert.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +# Author: wxnacy(wxnacy@gmail.com) +# Description: + +ipynb_files=`find . -name "*.ipynb" | grep -v checkpoints` +for file in $ipynb_files +do + outdir=`python -c "import os;print(os.path.dirname('${file}'.replace('./', './static/')))"` + jupyter nbconvert $file --output-dir $outdir +done diff --git a/notebook/create_index.py b/notebook/create_index.py new file mode 100644 index 0000000..3e33aa7 --- /dev/null +++ b/notebook/create_index.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +# -*- coding:utf-8 -*- +# Author: wxnacy(wxnacy@gmail.com) +# Description: + +HTML = """\ + + + + + Error response + + +

Directory listing for {}

+
+ {} +
+ + +""" + + +import os +import urllib.parse + +def main(dirname): + for dirname, subdirs, files in os.walk(dirname): + lines = '' + for subdir in subdirs: + url = urllib.parse.quote(subdir) + lines = f'{lines}
  • {subdir}/
  • ' + for f in files: + url = urllib.parse.quote(f) + lines = f'{lines}
  • {f}
  • ' + index = f'{dirname}/index.html' + os.remove(index) + with open(index, 'w') as f: + f.write(HTML.format(dirname.replace(f'{dirname}/', './'), f"")) + +if __name__ == "__main__": + main('./static') diff --git a/notebook/modules/databases/connections.ipynb b/notebook/modules/databases/connections.ipynb new file mode 100644 index 0000000..9a058c2 --- /dev/null +++ b/notebook/modules/databases/connections.ipynb @@ -0,0 +1,153 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(1, 'Daisy', 92)\n" + ] + } + ], + "source": [ + "# 您可以通过将数据库用作异步上下文管理器来控制数据库的连接/断开连接。\n", + "import config\n", + "from databases import Database\n", + "async with Database(config.DATABASE_URL) as database:\n", + " row = await database.fetch_one(query=\"select * from test_user;\")\n", + " print(row)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "'''\n", + "If you're integrating against a web framework, \n", + "then you'll probably want to hook into framework \n", + "startup or shutdown events. For example, with Starlette you would use the following:\n", + "'''\n", + "@app.on_event(\"startup\")\n", + "async def startup():\n", + " await database.connect()\n", + "\n", + "@app.on_event(\"shutdown\")\n", + "async def shutdown():\n", + " await database.disconnect()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 连接选项\n", + "# Use an SSL connection.\n", + "database = Database('postgresql://localhost/example?ssl=true')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Use a connection pool of between 5-20 connections.\n", + "database = Database('mysql://localhost/example?min_size=5&max_size=20')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 使用键值对设置选项\n", + "database = Database('postgresql://localhost/example', ssl=True, min_size=5, max_size=20)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "# 测试连接池\n", + "\n", + "from databases import Database\n", + "import multiprocessing as mp\n", + "import config\n", + "\n", + "database = Database(config.DATABASE_URL, min_size=2, max_size=5)\n", + "await database.connect()\n", + "\n", + "def sleep():\n", + " print(\"hw\")\n", + " # res = database.fetch_one(\"select sleep(1)\")\n", + " print(res)\n", + "\n", + "pool = mp.Pool(10)\n", + "for i in range(10):\n", + " pool.apply_async(sleep, ())\n", + "await database.disconnect()" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "ename": "AssertionError", + "evalue": "Already disconnected.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36masync-def-wrapper\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32m~/.pyenv/versions/3.7.6/Python.framework/Versions/3.7/lib/python3.7/site-packages/databases/core.py\u001b[0m in \u001b[0;36mdisconnect\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 103\u001b[0m \u001b[0mClose\u001b[0m \u001b[0mall\u001b[0m \u001b[0mconnections\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mthe\u001b[0m \u001b[0mconnection\u001b[0m \u001b[0mpool\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 104\u001b[0m \"\"\"\n\u001b[0;32m--> 105\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_connected\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"Already disconnected.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 106\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 107\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_force_rollback\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mAssertionError\u001b[0m: Already disconnected." + ] + } + ], + "source": [ + "await database.disconnect()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/notebook/modules/databases/quickstart.ipynb b/notebook/modules/databases/quickstart.ipynb new file mode 100644 index 0000000..732673b --- /dev/null +++ b/notebook/modules/databases/quickstart.ipynb @@ -0,0 +1,177 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Collecting databases[mysql]\n", + " Downloading databases-0.3.2-py3-none-any.whl (18 kB)\n", + "Requirement already satisfied: sqlalchemy in /Users/wxnacy/.pyenv/versions/3.7.6/Python.framework/Versions/3.7/lib/python3.7/site-packages (from databases[mysql]) (1.3.15)\n", + "Collecting aiomysql; extra == \"mysql\"\n", + " Downloading aiomysql-0.0.20-py3-none-any.whl (40 kB)\n", + "\u001b[K |████████████████████████████████| 40 kB 128 kB/s ta 0:00:01\n", + "\u001b[?25hRequirement already satisfied: pymysql; extra == \"mysql\" in /Users/wxnacy/.pyenv/versions/3.7.6/Python.framework/Versions/3.7/lib/python3.7/site-packages (from databases[mysql]) (0.9.3)\n", + "\u001b[31mERROR: aiomysql 0.0.20 has requirement PyMySQL<=0.9.2,>=0.9, but you'll have pymysql 0.9.3 which is incompatible.\u001b[0m\n", + "Installing collected packages: aiomysql, databases\n", + "Successfully installed aiomysql-0.0.20 databases-0.3.2\n" + ] + } + ], + "source": [ + "'''\n", + "https://www.encode.io/databases/\n", + "databases 为您提供了对一系列数据库的简单异步支持。\n", + "\n", + "它使您可以使用功能强大的SQLAlchemy Core表达式语言进行查询,并提供对PostgreSQL,MySQL和SQLite的支持。\n", + "\n", + "'''\n", + "\n", + "# 安装\n", + "!pip install databases[mysql]" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "# Create a database instance, and connect to it.\n", + "from databases import Database\n", + "import config\n", + "database = Database(config.DATABASE_URL)\n", + "await database.connect()" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Create a table.\n", + "query = \"\"\"\n", + "DROP TABLE IF EXISTS `test_user`;\n", + "CREATE TABLE `test_user` (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100), score INT);\n", + "\"\"\"\n", + "await database.execute(query=query)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "# Insert some data.\n", + "query = \"INSERT INTO test_user (name, score) VALUES (:name, :score)\"\n", + "values = [\n", + " {\"name\": \"Daisy\", \"score\": 92},\n", + " {\"name\": \"Neil\", \"score\": 87},\n", + " {\"name\": \"Carol\", \"score\": 43},\n", + "]\n", + "await database.execute_many(query=query, values=values)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(1, 'Daisy', 92), (2, 'Neil', 87), (3, 'Carol', 43)]" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Run a database query.\n", + "query = \"SELECT * FROM test_user\"\n", + "await database.fetch_all(query=query)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 'Daisy', 92)" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Fetch single row\n", + "query = \"SELECT * FROM test_user WHERE id = :id\"\n", + "await database.fetch_one(query=query, values={\"id\": 1})" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "# Close all connection in the connection pool\n", + "await database.disconnect()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/notebook/modules/databases/sqlalchemy.ipynb b/notebook/modules/databases/sqlalchemy.ipynb new file mode 100644 index 0000000..d687962 --- /dev/null +++ b/notebook/modules/databases/sqlalchemy.ipynb @@ -0,0 +1,255 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "# 使用 sqlalchemy 进行数据库操作\n", + "import sqlalchemy\n", + "from sqlalchemy import Column\n", + "from sqlalchemy import Integer\n", + "from sqlalchemy import String\n", + "from sqlalchemy import Boolean\n", + "import config\n", + "\n", + "\n", + "metadata = sqlalchemy.MetaData()\n", + "\n", + "user = sqlalchemy.Table(\n", + " \"test_user\",\n", + " metadata,\n", + " Column(\"id\", Integer, primary_key=True),\n", + " Column(\"name\", String(length=100)),\n", + " Column(\"score\", Integer),\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# Create a database instance, and connect to it.\n", + "from databases import Database\n", + "database = Database(config.DATABASE_URL)\n", + "await database.connect()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Execute\n", + "query = user.insert()\n", + "values = {\"name\": \"wxnacy\", \"score\": 100}\n", + "await database.execute(query=query, values=values)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "# Execute many\n", + "query = user.insert()\n", + "values = [\n", + " {\"name\": \"wxnacy\", \"score\": 100},\n", + " {\"name\": \"wen\", \"score\": 100}\n", + "]\n", + "await database.execute_many(query=query, values=values)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "([(1, 'Daisy', 92),\n", + " (2, 'Neil', 87),\n", + " (3, 'Carol', 43),\n", + " (4, 'wxnacy', 100),\n", + " (5, 'wxnacy', 100),\n", + " (6, 'wen', 100)],\n", + " list)" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Fetch multiple rows\n", + "query = user.select()\n", + "rows = await database.fetch_all(query=query)\n", + "rows, type(rows)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((1, 'Daisy', 92), sqlalchemy.engine.result.RowProxy, 1)" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Fetch single row\n", + "query = user.select()\n", + "row = await database.fetch_one(query=query)\n", + "row, type(row), row.id" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Fetch single value, defaults to `column=0`.\n", + "query = user.select()\n", + "await database.fetch_val(query=query)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(1, 'Daisy', 92)\n", + "(2, 'Neil', 87)\n", + "(3, 'Carol', 43)\n", + "(4, 'wxnacy', 100)\n", + "(5, 'wxnacy', 100)\n", + "(6, 'wen', 100)\n" + ] + } + ], + "source": [ + "# Fetch multiple rows without loading them all into memory at once\n", + "query = user.select()\n", + "async for row in database.iterate(query=query):\n", + " print(row)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on method select in module sqlalchemy.sql.selectable:\n", + "\n", + "select(whereclause=None, **params) method of sqlalchemy.sql.schema.Table instance\n", + " return a SELECT of this :class:`.FromClause`.\n", + " \n", + " .. seealso::\n", + " \n", + " :func:`~.sql.expression.select` - general purpose\n", + " method which allows for arbitrary column lists.\n", + "\n" + ] + } + ], + "source": [ + "help(user.select)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(1, 'Daisy', 92)]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from sqlalchemy import text\n", + "query = user.select(text(\"id = 1\"))\n", + "await database.fetch_all(query=query)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/notebook/modules/faker/command.ipynb b/notebook/modules/faker/command.ipynb new file mode 100644 index 0000000..e83e387 --- /dev/null +++ b/notebook/modules/faker/command.ipynb @@ -0,0 +1,163 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Chris Curtis\r\n", + "\r\n" + ] + } + ], + "source": [ + "# 生成名字\n", + "!faker name" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "窦鑫\r\n", + "\r\n" + ] + } + ], + "source": [ + "# 设置语言\n", + "!faker -l zh_CN name" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Victor Martinez;\n", + "Brenda Johnson;\n", + "Timothy Bridges;\n" + ] + } + ], + "source": [ + "# 重复三次,并设置后缀\n", + "!faker -r 3 -s \";\" name" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "usage: faker [-h] [--version] [-v] [-o output] [-l LOCALE] [-r REPEAT]\n", + " [-s SEP] [--seed SEED] [-i [INCLUDE [INCLUDE ...]]]\n", + " [fake] [fake argument [fake argument ...]]\n", + "\n", + "faker version 4.1.0\n", + "\n", + "positional arguments:\n", + " fake name of the fake to generate output for (e.g. profile)\n", + " fake argument optional arguments to pass to the fake (e.g. the\n", + " profile fake takes an optional list of comma separated\n", + " field names as the first argument)\n", + "\n", + "optional arguments:\n", + " -h, --help show this help message and exit\n", + " --version show program's version number and exit\n", + " -v, --verbose show INFO logging events instead of CRITICAL, which is\n", + " the default. These logging events provide insight into\n", + " localization of specific providers.\n", + " -o output redirect output to a file\n", + " -l LOCALE, --lang LOCALE\n", + " specify the language for a localized provider (e.g.\n", + " de_DE)\n", + " -r REPEAT, --repeat REPEAT\n", + " generate the specified number of outputs\n", + " -s SEP, --sep SEP use the specified separator after each output\n", + " --seed SEED specify a seed for the random generator so that\n", + " results are repeatable. Also compatible with 'repeat'\n", + " option\n", + " -i [INCLUDE [INCLUDE ...]], --include [INCLUDE [INCLUDE ...]]\n", + " list of additional custom providers to user, given as\n", + " the import path of the module containing your Provider\n", + " class (not the provider class itself)\n", + "\n", + "supported locales:\n", + "\n", + " ar_AA, ar_EG, ar_JO, ar_PS, ar_SA, bg_BG, bs_BA, cs_CZ, de, de_AT, de_CH, de_DE, dk_DK, el_CY, el_GR, en, en_AU, en_CA, en_GB, en_IE, en_IN, en_NZ, en_PH, en_TH, en_US, es, es_CA, es_ES, es_MX, et_EE, fa_IR, fi_FI, fil_PH, fr_CH, fr_FR, fr_QC, he_IL, hi_IN, hr_HR, hu_HU, hy_AM, id_ID, it_IT, ja_JP, ka_GE, ko_KR, la, lb_LU, lt_LT, lv_LV, mt_MT, ne_NP, nl_BE, nl_NL, no_NO, pl_PL, pt_BR, pt_PT, ro_RO, ru_RU, sk_SK, sl_SI, sv_SE, ta_IN, th_TH, tl_PH, tr_TR, tw_GH, uk_UA, zh_CN, zh_TW\n", + "\n", + " Faker can take a locale as an optional argument, to return localized data. If\n", + " no locale argument is specified, the factory falls back to the user's OS\n", + " locale as long as it is supported by at least one of the providers.\n", + " - for this user, the default locale is en_US.\n", + "\n", + " If the optional argument locale and/or user's default locale is not available\n", + " for the specified provider, the factory falls back to faker's default locale,\n", + " which is en_US.\n", + "\n", + "examples:\n", + "\n", + " $ faker address\n", + " 968 Bahringer Garden Apt. 722\n", + " Kristinaland, NJ 09890\n", + "\n", + " $ faker -l de_DE address\n", + " Samira-Niemeier-Allee 56\n", + " 94812 Biedenkopf\n", + "\n", + " $ faker profile ssn,birthdate\n", + " {'ssn': u'628-10-1085', 'birthdate': '2008-03-29'}\n", + "\n", + " $ faker -r=3 -s=\";\" name\n", + " Willam Kertzmann;\n", + " Josiah Maggio;\n", + " Gayla Schmitt;\n" + ] + } + ], + "source": [ + "# 帮助文档\n", + "!faker -h" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/notebook/modules/faker/locales.ipynb b/notebook/modules/faker/locales.ipynb new file mode 100644 index 0000000..321251d --- /dev/null +++ b/notebook/modules/faker/locales.ipynb @@ -0,0 +1,146 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'李颖'" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from faker import Faker\n", + "\n", + "# 设置中文\n", + "fake = Faker('zh_CN')\n", + "fake.name()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "叶柳\n", + "田中 充\n", + "Thomas Decker\n", + "Kyle Avila\n", + "何勇\n", + "田辺 結衣\n", + "Ashley Gonzalez\n", + "梁莹\n", + "Cheryl Long\n", + "大垣 治\n" + ] + } + ], + "source": [ + "# 设置多个语言\n", + "fake = Faker(['zh_CN', 'ja_JP', 'en_US'])\n", + "for _ in range(10):\n", + " print(fake.name())" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "# 设置多语言的权重\n", + "\n", + "from collections import OrderedDict\n", + "locales = OrderedDict([\n", + " ('en-US', 1),\n", + " ('en-PH', 2),\n", + " ('ja_JP', 3),\n", + "])\n", + "fake = Faker(locales)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['en_US', 'en_PH', 'ja_JP']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 获取指定的语言列表\n", + "fake.locales" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Nicholas Malone'" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 单独获取列表中某一个语言\n", + "fake['en-US'].name()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/notebook/modules/faker/quickstart.ipynb b/notebook/modules/faker/quickstart.ipynb new file mode 100644 index 0000000..14e6553 --- /dev/null +++ b/notebook/modules/faker/quickstart.ipynb @@ -0,0 +1,488 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Michelle Little'" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#!/usr/bin/env python\n", + "# -*- coding:utf-8 -*-\n", + "# Author: wxnacy(wxnacy@gmail.com)\n", + "# Faker 快速开始\n", + "# https://github.com/joke2k/faker\n", + "\n", + "from faker import Faker\n", + "fake = Faker()\n", + "# 生成名字\n", + "fake.name()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'0226 Donald Courts\\nColeborough, WA 70527'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 生成地址\n", + "fake.address()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Age political even bad coach success. List through hand.\\nDo animal with office born source. Base agree feeling human hour. Answer player structure glass.\\nBusiness issue plant box.'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 生成文本\n", + "fake.text()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Opera/9.26.(X11; Linux x86_64; om-KE) Presto/2.9.171 Version/11.00'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 生成 user_agent\n", + "fake.user_agent()" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'1.182.26.39'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 生成 ipv4\n", + "fake.ipv4()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Android 2.2.2'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 生成 android 平台名称\n", + "fake.android_platform_token()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'iPad; CPU iPad OS 7_1_2 like Mac OS X'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 生成 ios 平台名称\n", + "fake.ios_platform_token()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__class__',\n", + " '__delattr__',\n", + " '__dict__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattr__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__le__',\n", + " '__lt__',\n", + " '__module__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " '__weakref__',\n", + " '_factories',\n", + " '_factory_map',\n", + " '_locales',\n", + " '_map_provider_method',\n", + " '_select_factory',\n", + " '_weights',\n", + " 'add_provider',\n", + " 'address',\n", + " 'am_pm',\n", + " 'android_platform_token',\n", + " 'ascii_company_email',\n", + " 'ascii_email',\n", + " 'ascii_free_email',\n", + " 'ascii_safe_email',\n", + " 'bank_country',\n", + " 'bban',\n", + " 'binary',\n", + " 'boolean',\n", + " 'bothify',\n", + " 'bs',\n", + " 'building_number',\n", + " 'cache_pattern',\n", + " 'catch_phrase',\n", + " 'century',\n", + " 'chrome',\n", + " 'city',\n", + " 'city_prefix',\n", + " 'city_suffix',\n", + " 'color',\n", + " 'color_name',\n", + " 'company',\n", + " 'company_email',\n", + " 'company_suffix',\n", + " 'coordinate',\n", + " 'country',\n", + " 'country_calling_code',\n", + " 'country_code',\n", + " 'credit_card_expire',\n", + " 'credit_card_full',\n", + " 'credit_card_number',\n", + " 'credit_card_provider',\n", + " 'credit_card_security_code',\n", + " 'cryptocurrency',\n", + " 'cryptocurrency_code',\n", + " 'cryptocurrency_name',\n", + " 'csv',\n", + " 'currency',\n", + " 'currency_code',\n", + " 'currency_name',\n", + " 'currency_symbol',\n", + " 'date',\n", + " 'date_between',\n", + " 'date_between_dates',\n", + " 'date_object',\n", + " 'date_of_birth',\n", + " 'date_this_century',\n", + " 'date_this_decade',\n", + " 'date_this_month',\n", + " 'date_this_year',\n", + " 'date_time',\n", + " 'date_time_ad',\n", + " 'date_time_between',\n", + " 'date_time_between_dates',\n", + " 'date_time_this_century',\n", + " 'date_time_this_decade',\n", + " 'date_time_this_month',\n", + " 'date_time_this_year',\n", + " 'day_of_month',\n", + " 'day_of_week',\n", + " 'dga',\n", + " 'domain_name',\n", + " 'domain_word',\n", + " 'dsv',\n", + " 'ean',\n", + " 'ean13',\n", + " 'ean8',\n", + " 'ein',\n", + " 'email',\n", + " 'factories',\n", + " 'file_extension',\n", + " 'file_name',\n", + " 'file_path',\n", + " 'firefox',\n", + " 'first_name',\n", + " 'first_name_female',\n", + " 'first_name_male',\n", + " 'format',\n", + " 'free_email',\n", + " 'free_email_domain',\n", + " 'future_date',\n", + " 'future_datetime',\n", + " 'generator_attrs',\n", + " 'get_formatter',\n", + " 'get_providers',\n", + " 'hex_color',\n", + " 'hexify',\n", + " 'hostname',\n", + " 'http_method',\n", + " 'iban',\n", + " 'image_url',\n", + " 'internet_explorer',\n", + " 'invalid_ssn',\n", + " 'ios_platform_token',\n", + " 'ipv4',\n", + " 'ipv4_network_class',\n", + " 'ipv4_private',\n", + " 'ipv4_public',\n", + " 'ipv6',\n", + " 'isbn10',\n", + " 'isbn13',\n", + " 'iso8601',\n", + " 'items',\n", + " 'itin',\n", + " 'job',\n", + " 'language_code',\n", + " 'last_name',\n", + " 'last_name_female',\n", + " 'last_name_male',\n", + " 'latitude',\n", + " 'latlng',\n", + " 'lexify',\n", + " 'license_plate',\n", + " 'linux_platform_token',\n", + " 'linux_processor',\n", + " 'local_latlng',\n", + " 'locale',\n", + " 'locales',\n", + " 'location_on_land',\n", + " 'longitude',\n", + " 'mac_address',\n", + " 'mac_platform_token',\n", + " 'mac_processor',\n", + " 'md5',\n", + " 'military_apo',\n", + " 'military_dpo',\n", + " 'military_ship',\n", + " 'military_state',\n", + " 'mime_type',\n", + " 'month',\n", + " 'month_name',\n", + " 'msisdn',\n", + " 'name',\n", + " 'name_female',\n", + " 'name_male',\n", + " 'null_boolean',\n", + " 'numerify',\n", + " 'opera',\n", + " 'paragraph',\n", + " 'paragraphs',\n", + " 'parse',\n", + " 'password',\n", + " 'past_date',\n", + " 'past_datetime',\n", + " 'phone_number',\n", + " 'port_number',\n", + " 'postalcode',\n", + " 'postalcode_in_state',\n", + " 'postalcode_plus4',\n", + " 'postcode',\n", + " 'postcode_in_state',\n", + " 'prefix',\n", + " 'prefix_female',\n", + " 'prefix_male',\n", + " 'profile',\n", + " 'provider',\n", + " 'providers',\n", + " 'psv',\n", + " 'pybool',\n", + " 'pydecimal',\n", + " 'pydict',\n", + " 'pyfloat',\n", + " 'pyint',\n", + " 'pyiterable',\n", + " 'pylist',\n", + " 'pyset',\n", + " 'pystr',\n", + " 'pystr_format',\n", + " 'pystruct',\n", + " 'pytuple',\n", + " 'random',\n", + " 'random_choices',\n", + " 'random_digit',\n", + " 'random_digit_not_null',\n", + " 'random_digit_not_null_or_empty',\n", + " 'random_digit_or_empty',\n", + " 'random_element',\n", + " 'random_elements',\n", + " 'random_int',\n", + " 'random_letter',\n", + " 'random_letters',\n", + " 'random_lowercase_letter',\n", + " 'random_number',\n", + " 'random_sample',\n", + " 'random_uppercase_letter',\n", + " 'randomize_nb_elements',\n", + " 'rgb_color',\n", + " 'rgb_css_color',\n", + " 'safari',\n", + " 'safe_color_name',\n", + " 'safe_email',\n", + " 'safe_hex_color',\n", + " 'secondary_address',\n", + " 'seed',\n", + " 'seed_instance',\n", + " 'seed_locale',\n", + " 'sentence',\n", + " 'sentences',\n", + " 'set_formatter',\n", + " 'sha1',\n", + " 'sha256',\n", + " 'simple_profile',\n", + " 'slug',\n", + " 'ssn',\n", + " 'state',\n", + " 'state_abbr',\n", + " 'street_address',\n", + " 'street_name',\n", + " 'street_suffix',\n", + " 'suffix',\n", + " 'suffix_female',\n", + " 'suffix_male',\n", + " 'tar',\n", + " 'text',\n", + " 'texts',\n", + " 'time',\n", + " 'time_delta',\n", + " 'time_object',\n", + " 'time_series',\n", + " 'timezone',\n", + " 'tld',\n", + " 'tsv',\n", + " 'unix_device',\n", + " 'unix_partition',\n", + " 'unix_time',\n", + " 'upc_a',\n", + " 'upc_e',\n", + " 'uri',\n", + " 'uri_extension',\n", + " 'uri_page',\n", + " 'uri_path',\n", + " 'url',\n", + " 'user_agent',\n", + " 'user_name',\n", + " 'uuid4',\n", + " 'weights',\n", + " 'windows_platform_token',\n", + " 'word',\n", + " 'words',\n", + " 'year',\n", + " 'zip',\n", + " 'zipcode',\n", + " 'zipcode_in_state',\n", + " 'zipcode_plus4']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 查看 faker 属性\n", + "dir(fake)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/notebook/modules/fastapi/Untitled.ipynb b/notebook/modules/fastapi/Untitled.ipynb new file mode 100644 index 0000000..b95c9d6 --- /dev/null +++ b/notebook/modules/fastapi/Untitled.ipynb @@ -0,0 +1,49 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[33mWARNING: Package(s) not found: databases\u001b[0m\r\n" + ] + } + ], + "source": [ + "!pip show databases" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/notebook/modules/hashlib.ipynb b/notebook/modules/hashlib.ipynb new file mode 100644 index 0000000..773335d --- /dev/null +++ b/notebook/modules/hashlib.ipynb @@ -0,0 +1,195 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'1f806eb48b670c40af49a3f764ba086f'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import hashlib\n", + "\n", + "# 计算字符串 md5 值\n", + "message = \"wxnacy\"\n", + "md5 = hashlib.md5()\n", + "md5.update(message.encode('utf-8'))\n", + "md5.hexdigest()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'1f806eb48b670c40af49a3f764ba086f'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 计算字符串 md5 值\n", + "hashlib.md5(message.encode('utf-8')).hexdigest()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'f0629f11bf649b31f040ec4248818cba'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 计算文件 md5 值\n", + "def encrypt_file(filename):\n", + " hash_md5 = hashlib.md5()\n", + " with open(filename, \"rb\") as f:\n", + " for chunk in iter(lambda: f.read(4096), b\"\"):\n", + " hash_md5.update(chunk)\n", + " return hash_md5.hexdigest()\n", + "\n", + "encrypt_file('./hashlib.ipynb')" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'7943484aaae8ec07556c23a41be3cdbe'" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 计算网址内容的 md5 值\n", + "from urllib.request import urlopen\n", + "\n", + "def encrypt_url(url, max_file_size=100 * 1024 * 1024):\n", + " remote = urlopen(url)\n", + " h = hashlib.md5()\n", + "\n", + " total_read = 0\n", + " while True:\n", + " data = remote.read(4096)\n", + " total_read += 4096\n", + "\n", + " if not data or total_read > max_file_size:\n", + " break\n", + " h.update(data)\n", + " return h.hexdigest()\n", + "\n", + "encrypt_url(\"https://wxnacy.com\")" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['__all__',\n", + " '__builtin_constructor_cache',\n", + " '__builtins__',\n", + " '__cached__',\n", + " '__doc__',\n", + " '__file__',\n", + " '__get_builtin_constructor',\n", + " '__loader__',\n", + " '__name__',\n", + " '__package__',\n", + " '__spec__',\n", + " '_hashlib',\n", + " 'algorithms_available',\n", + " 'algorithms_guaranteed',\n", + " 'blake2b',\n", + " 'blake2s',\n", + " 'md5',\n", + " 'new',\n", + " 'pbkdf2_hmac',\n", + " 'scrypt',\n", + " 'sha1',\n", + " 'sha224',\n", + " 'sha256',\n", + " 'sha384',\n", + " 'sha3_224',\n", + " 'sha3_256',\n", + " 'sha3_384',\n", + " 'sha3_512',\n", + " 'sha512',\n", + " 'shake_128',\n", + " 'shake_256']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 查看其它 hash 值算法\n", + "dir(hashlib)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/notebook/modules/hmac.ipynb b/notebook/modules/hmac.ipynb new file mode 100644 index 0000000..4a24794 --- /dev/null +++ b/notebook/modules/hmac.ipynb @@ -0,0 +1,83 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'56c83dc453f66c4d4bf05f8ab65179f12b7410e2'" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 计算 hmac_sha1 加密值\n", + "import hmac\n", + "import hashlib\n", + "\n", + "message = 'wxnacy'\n", + "key = 'wxnacy.com'\n", + "\n", + "h = hmac.new(key.encode('utf-8'), message.encode('utf-8'), hashlib.sha1)\n", + "# 计算 16 进制编码字符串\n", + "h.hexdigest()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Vsg9xFP2bE1L8F+KtlF58St0EOI='" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 使用 base64 将 hmac 结果编码为字符串\n", + "import base64\n", + "base64.b64encode(h.digest()).decode()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/notebook/modules/sqlalchemy/Untitled.ipynb b/notebook/modules/sqlalchemy/Untitled.ipynb new file mode 100644 index 0000000..2c66430 --- /dev/null +++ b/notebook/modules/sqlalchemy/Untitled.ipynb @@ -0,0 +1,353 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Collecting mysqlclient\n", + " Using cached mysqlclient-1.4.6.tar.gz (85 kB)\n", + "Using legacy setup.py install for mysqlclient, since package 'wheel' is not installed.\n", + "Installing collected packages: mysqlclient\n", + " Running setup.py install for mysqlclient ... \u001b[?25lerror\n", + "\u001b[31m ERROR: Command errored out with exit status 1:\n", + " command: /Users/wxnacy/.pyenv/versions/3.7.6/envs/notebook/bin/python3.7 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '\"'\"'/private/var/folders/kz/8syfctw919zdt3shr9w5j8v00000gn/T/pip-install-gzuju0kh/mysqlclient/setup.py'\"'\"'; __file__='\"'\"'/private/var/folders/kz/8syfctw919zdt3shr9w5j8v00000gn/T/pip-install-gzuju0kh/mysqlclient/setup.py'\"'\"';f=getattr(tokenize, '\"'\"'open'\"'\"', open)(__file__);code=f.read().replace('\"'\"'\\r\\n'\"'\"', '\"'\"'\\n'\"'\"');f.close();exec(compile(code, __file__, '\"'\"'exec'\"'\"'))' install --record /private/var/folders/kz/8syfctw919zdt3shr9w5j8v00000gn/T/pip-record-7d7ia_ag/install-record.txt --single-version-externally-managed --compile --install-headers /Users/wxnacy/.pyenv/versions/3.7.6/envs/notebook/include/site/python3.7/mysqlclient\n", + " cwd: /private/var/folders/kz/8syfctw919zdt3shr9w5j8v00000gn/T/pip-install-gzuju0kh/mysqlclient/\n", + " Complete output (30 lines):\n", + " running install\n", + " running build\n", + " running build_py\n", + " creating build\n", + " creating build/lib.macosx-10.14-x86_64-3.7\n", + " creating build/lib.macosx-10.14-x86_64-3.7/MySQLdb\n", + " copying MySQLdb/__init__.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb\n", + " copying MySQLdb/_exceptions.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb\n", + " copying MySQLdb/compat.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb\n", + " copying MySQLdb/connections.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb\n", + " copying MySQLdb/converters.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb\n", + " copying MySQLdb/cursors.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb\n", + " copying MySQLdb/release.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb\n", + " copying MySQLdb/times.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb\n", + " creating build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants\n", + " copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants\n", + " copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants\n", + " copying MySQLdb/constants/CR.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants\n", + " copying MySQLdb/constants/ER.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants\n", + " copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants\n", + " copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants\n", + " running build_ext\n", + " building 'MySQLdb._mysql' extension\n", + " creating build/temp.macosx-10.14-x86_64-3.7\n", + " creating build/temp.macosx-10.14-x86_64-3.7/MySQLdb\n", + " clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -Dversion_info=(1,4,6,'final',0) -D__version__=1.4.6 -I/usr/local/Cellar/mysql/5.7.19/include/mysql -I/Users/wxnacy/.pyenv/versions/3.7.6/envs/notebook/include -I/Users/wxnacy/.pyenv/versions/3.7.6/Python.framework/Versions/3.7/include/python3.7m -c MySQLdb/_mysql.c -o build/temp.macosx-10.14-x86_64-3.7/MySQLdb/_mysql.o\n", + " clang -bundle -undefined dynamic_lookup -L/usr/local/opt/readline/lib -L/usr/local/opt/readline/lib -L/Users/wxnacy/.pyenv/versions/3.7.6/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/readline/lib -L/Users/wxnacy/.pyenv/versions/3.7.6/lib build/temp.macosx-10.14-x86_64-3.7/MySQLdb/_mysql.o -L/usr/local/Cellar/mysql/5.7.19/lib -lmysqlclient -lssl -lcrypto -o build/lib.macosx-10.14-x86_64-3.7/MySQLdb/_mysql.cpython-37m-darwin.so\n", + " ld: library not found for -lssl\n", + " clang: error: linker command failed with exit code 1 (use -v to see invocation)\n", + " error: command 'clang' failed with exit status 1\n", + " ----------------------------------------\u001b[0m\n", + "\u001b[31mERROR: Command errored out with exit status 1: /Users/wxnacy/.pyenv/versions/3.7.6/envs/notebook/bin/python3.7 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '\"'\"'/private/var/folders/kz/8syfctw919zdt3shr9w5j8v00000gn/T/pip-install-gzuju0kh/mysqlclient/setup.py'\"'\"'; __file__='\"'\"'/private/var/folders/kz/8syfctw919zdt3shr9w5j8v00000gn/T/pip-install-gzuju0kh/mysqlclient/setup.py'\"'\"';f=getattr(tokenize, '\"'\"'open'\"'\"', open)(__file__);code=f.read().replace('\"'\"'\\r\\n'\"'\"', '\"'\"'\\n'\"'\"');f.close();exec(compile(code, __file__, '\"'\"'exec'\"'\"'))' install --record /private/var/folders/kz/8syfctw919zdt3shr9w5j8v00000gn/T/pip-record-7d7ia_ag/install-record.txt --single-version-externally-managed --compile --install-headers /Users/wxnacy/.pyenv/versions/3.7.6/envs/notebook/include/site/python3.7/mysqlclient Check the logs for full command output.\u001b[0m\n", + "\u001b[?25h" + ] + } + ], + "source": [ + "!pip install mysqlclient" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "ename": "ModuleNotFoundError", + "evalue": "No module named 'MySQLdb'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m engine = create_engine(\n\u001b[0;32m---> 10\u001b[0;31m \u001b[0mSQLALCHEMY_DATABASE_URL\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mconnect_args\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m\"check_same_thread\"\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mFalse\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 11\u001b[0m )\n\u001b[1;32m 12\u001b[0m \u001b[0mSessionLocal\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msessionmaker\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mautocommit\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mautoflush\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbind\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mengine\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m~/.pyenv/versions/3.7.6/envs/notebook/lib/python3.7/site-packages/sqlalchemy/engine/__init__.py\u001b[0m in \u001b[0;36mcreate_engine\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 486\u001b[0m \u001b[0mstrategy\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"strategy\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdefault_strategy\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 487\u001b[0m \u001b[0mstrategy\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstrategies\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstrategies\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mstrategy\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 488\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mstrategy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcreate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 489\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 490\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m~/.pyenv/versions/3.7.6/envs/notebook/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py\u001b[0m in \u001b[0;36mcreate\u001b[0;34m(self, name_or_url, **kwargs)\u001b[0m\n\u001b[1;32m 85\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mk\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 86\u001b[0m \u001b[0mdbapi_args\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpop_kwarg\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 87\u001b[0;31m \u001b[0mdbapi\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdialect_cls\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdbapi\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mdbapi_args\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 88\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 89\u001b[0m \u001b[0mdialect_args\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"dbapi\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdbapi\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m~/.pyenv/versions/3.7.6/envs/notebook/lib/python3.7/site-packages/sqlalchemy/dialects/mysql/mysqldb.py\u001b[0m in \u001b[0;36mdbapi\u001b[0;34m(cls)\u001b[0m\n\u001b[1;32m 116\u001b[0m \u001b[0;34m@\u001b[0m\u001b[0mclassmethod\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 117\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mdbapi\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcls\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 118\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m__import__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"MySQLdb\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 119\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 120\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mon_connect\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'MySQLdb'" + ] + } + ], + "source": [ + "# pip install sqlalchemy pymysql \n", + "from sqlalchemy import create_engine\n", + "from sqlalchemy.ext.declarative import declarative_base\n", + "from sqlalchemy.orm import sessionmaker\n", + "import config\n", + "\n", + "SQLALCHEMY_DATABASE_URL = config.DATABASE_URL\n", + "\n", + "engine = create_engine(\n", + " SQLALCHEMY_DATABASE_URL, connect_args={\"check_same_thread\": False}\n", + ")\n", + "SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)\n", + "\n", + "Base = declarative_base()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from sqlalchemy import Boolean, Column, ForeignKey, Integer, String\n", + "\n", + "class User(Base):\n", + " __tablename__ = 'test_user'\n", + " id = Column(Integer, primary_key = True)\n", + " name = Column(String, default=\"\")\n", + " score = Column(Integer, default=0)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ARRAY', 'BIGINT', 'BINARY', 'BLANK_SCHEMA', 'BLOB', 'BOOLEAN', 'BigInteger', 'Binary', 'Boolean', 'CHAR', 'CLOB', 'CheckConstraint', 'Column', 'ColumnDefault', 'Computed', 'Constraint', 'DATE', 'DATETIME', 'DDL', 'DECIMAL', 'Date', 'DateTime', 'DefaultClause', 'Enum', 'FLOAT', 'FetchedValue', 'Float', 'ForeignKey', 'ForeignKeyConstraint', 'INT', 'INTEGER', 'Index', 'Integer', 'Interval', 'JSON', 'LargeBinary', 'MetaData', 'NCHAR', 'NUMERIC', 'NVARCHAR', 'Numeric', 'PassiveDefault', 'PickleType', 'PrimaryKeyConstraint', 'REAL', 'SMALLINT', 'Sequence', 'SmallInteger', 'String', 'TEXT', 'TIME', 'TIMESTAMP', 'Table', 'Text', 'ThreadLocalMetaData', 'Time', 'TypeDecorator', 'Unicode', 'UnicodeText', 'UniqueConstraint', 'VARBINARY', 'VARCHAR', 'alias', 'all_', 'and_', 'any_', 'asc', 'between', 'bindparam', 'case', 'cast', 'collate', 'column', 'create_engine', 'delete', 'desc', 'distinct', 'engine_from_config', 'except_', 'except_all', 'exists', 'extract', 'false', 'func', 'funcfilter', 'insert', 'inspect', 'intersect', 'intersect_all', 'join', 'lateral', 'literal', 'literal_column', 'modifier', 'not_', 'null', 'nullsfirst', 'nullslast', 'or_', 'outerjoin', 'outparam', 'over', 'select', 'subquery', 'table', 'tablesample', 'text', 'true', 'tuple_', 'type_coerce', 'union', 'union_all', 'update', 'within_group']\n", + "ARRAY\n", + "BIGINT\n", + "BINARY\n", + "BLANK_SCHEMA\n", + "BLOB\n", + "BOOLEAN\n", + "BigInteger\n", + "Binary\n", + "Boolean\n", + "CHAR\n", + "CLOB\n", + "CheckConstraint\n", + "Column\n", + "ColumnDefault\n", + "Computed\n", + "Constraint\n", + "DATE\n", + "DATETIME\n", + "DDL\n", + "DECIMAL\n", + "Date\n", + "DateTime\n", + "DefaultClause\n", + "Enum\n", + "FLOAT\n", + "FetchedValue\n", + "Float\n", + "ForeignKey\n", + "ForeignKeyConstraint\n", + "INT\n", + "INTEGER\n", + "Index\n", + "Integer\n", + "Interval\n", + "JSON\n", + "LargeBinary\n", + "MetaData\n", + "NCHAR\n", + "NUMERIC\n", + "NVARCHAR\n", + "Numeric\n", + "PassiveDefault\n", + "PickleType\n", + "PrimaryKeyConstraint\n", + "REAL\n", + "SMALLINT\n", + "Sequence\n", + "SmallInteger\n", + "String\n", + "TEXT\n", + "TIME\n", + "TIMESTAMP\n", + "Table\n", + "Text\n", + "ThreadLocalMetaData\n", + "Time\n", + "TypeDecorator\n", + "Unicode\n", + "UnicodeText\n", + "UniqueConstraint\n", + "VARBINARY\n", + "VARCHAR\n", + "alias\n", + "all_\n", + "and_\n", + "any_\n", + "asc\n", + "between\n", + "bindparam\n", + "case\n", + "cast\n", + "collate\n", + "column\n", + "create_engine\n", + "delete\n", + "desc\n", + "distinct\n", + "engine_from_config\n", + "except_\n", + "except_all\n", + "exists\n", + "extract\n", + "false\n", + "func\n", + "funcfilter\n", + "insert\n", + "inspect\n", + "intersect\n", + "intersect_all\n", + "join\n", + "lateral\n", + "literal\n", + "literal_column\n", + "modifier\n", + "not_\n", + "null\n", + "nullsfirst\n", + "nullslast\n", + "or_\n", + "outerjoin\n", + "outparam\n", + "over\n", + "select\n", + "subquery\n", + "table\n", + "tablesample\n", + "text\n", + "true\n", + "tuple_\n", + "type_coerce\n", + "union\n", + "union_all\n", + "update\n", + "within_group\n", + "['AliasOption', 'AttributeExtension', 'Bundle', 'ColumnProperty', 'ComparableProperty', 'CompositeProperty', 'EXT_CONTINUE', 'EXT_SKIP', 'EXT_STOP', 'Load', 'Mapper', 'MapperExtension', 'PropComparator', 'Query', 'RelationshipProperty', 'Session', 'SessionExtension', 'SynonymProperty', 'aliased', 'backref', 'class_mapper', 'clear_mappers', 'close_all_sessions', 'column_property', 'comparable_property', 'compile_mappers', 'composite', 'configure_mappers', 'contains_alias', 'contains_eager', 'create_session', 'defaultload', 'defer', 'deferred', 'dynamic_loader', 'eagerload', 'eagerload_all', 'foreign', 'immediateload', 'join', 'joinedload', 'joinedload_all', 'lazyload', 'lazyload_all', 'load_only', 'make_transient', 'make_transient_to_detached', 'mapper', 'noload', 'object_mapper', 'object_session', 'outerjoin', 'polymorphic_union', 'public_factory', 'query_expression', 'raiseload', 'reconstructor', 'relation', 'relationship', 'remote', 'scoped_session', 'selectin_polymorphic', 'selectinload', 'selectinload_all', 'sessionmaker', 'subqueryload', 'subqueryload_all', 'synonym', 'undefer', 'undefer_group', 'validates', 'was_deleted', 'with_expression', 'with_parent', 'with_polymorphic']\n", + "AliasOption\n", + "AttributeExtension\n", + "Bundle\n", + "ColumnProperty\n", + "ComparableProperty\n", + "CompositeProperty\n", + "EXT_CONTINUE\n", + "EXT_SKIP\n", + "EXT_STOP\n", + "Load\n", + "Mapper\n", + "MapperExtension\n", + "PropComparator\n", + "Query\n", + "RelationshipProperty\n", + "Session\n", + "SessionExtension\n", + "SynonymProperty\n", + "aliased\n", + "backref\n", + "class_mapper\n", + "clear_mappers\n", + "close_all_sessions\n", + "column_property\n", + "comparable_property\n", + "compile_mappers\n", + "composite\n", + "configure_mappers\n", + "contains_alias\n", + "contains_eager\n", + "create_session\n", + "defaultload\n", + "defer\n", + "deferred\n", + "dynamic_loader\n", + "eagerload\n", + "eagerload_all\n", + "foreign\n", + "immediateload\n", + "join\n", + "joinedload\n", + "joinedload_all\n", + "lazyload\n", + "lazyload_all\n", + "load_only\n", + "make_transient\n", + "make_transient_to_detached\n", + "mapper\n", + "noload\n", + "object_mapper\n", + "object_session\n", + "outerjoin\n", + "polymorphic_union\n", + "public_factory\n", + "query_expression\n", + "raiseload\n", + "reconstructor\n", + "relation\n", + "relationship\n", + "remote\n", + "scoped_session\n", + "selectin_polymorphic\n", + "selectinload\n", + "selectinload_all\n", + "sessionmaker\n", + "subqueryload\n", + "subqueryload_all\n", + "synonym\n", + "undefer\n", + "undefer_group\n", + "validates\n", + "was_deleted\n", + "with_expression\n", + "with_parent\n", + "with_polymorphic\n" + ] + } + ], + "source": [ + "import sqlalchemy\n", + "for module in sqlalchemy, sqlalchemy.orm:\n", + " print(module.__all__)\n", + " for key in module.__all__:\n", + " print(key)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/notebook/modules/urlencode.ipynb b/notebook/modules/urlencode.ipynb new file mode 100644 index 0000000..feece48 --- /dev/null +++ b/notebook/modules/urlencode.ipynb @@ -0,0 +1,176 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'w%20xnacy'" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# https://www.urlencoder.io/python/\n", + "import urllib\n", + "urllib.parse.quote('w xnacy')" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'w+xnacy'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "urllib.parse.quote_plus('w xnacy')" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'/'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "urllib.parse.quote('/')" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'%2F'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "urllib.parse.quote('/', safe='')" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'text=name+is+wxnacy&url=https%3A%2F%2Fwxnacy.com'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "params = dict(text = 'name is wxnacy', url='https://wxnacy.com')\n", + "urllib.parse.urlencode(params)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'text=name%20is%20wxnacy&url=https%3A%2F%2Fwxnacy.com'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "urllib.parse.urlencode(params, quote_via=urllib.parse.quote)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'name=Rajeev+Singh&phone=%2B919999999999&phone=%2B628888888888'" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "params = {'name': 'Rajeev Singh', 'phone': ['+919999999999', '+628888888888']}\n", + "urllib.parse.urlencode(params, doseq=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/notebook/pm2_config.json b/notebook/pm2_config.json new file mode 100644 index 0000000..045910a --- /dev/null +++ b/notebook/pm2_config.json @@ -0,0 +1,7 @@ +{ + "apps" : [{ + "name" : "notebook", + "script" : "./run.sh", + "interpreter" : "/bin/bash", + }] +} diff --git a/notebook/publish.sh b/notebook/publish.sh new file mode 100755 index 0000000..b391116 --- /dev/null +++ b/notebook/publish.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +# Author: wxnacy(wxnacy@gmail.com) +# Description: + +ipynb_files=`find . -name "*.ipynb" | grep -v checkpoints` +for file in `find static -name "*.html"` +do + outfile=`python -c "print('${file}'.replace('static/', ''))"` + ossutil cp -u $file oss://notebook-cn/${outfile} +done diff --git a/notebook/run.sh b/notebook/run.sh new file mode 100755 index 0000000..17acb85 --- /dev/null +++ b/notebook/run.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +# Author: wxnacy(wxnacy@gmail.com) +# Description: + +PYTHONPATH=${PWD} jupyter notebook -y --port 6689 diff --git a/notebook/static/LeetCode/index.html b/notebook/static/LeetCode/index.html new file mode 100644 index 0000000..b62f1a3 --- /dev/null +++ b/notebook/static/LeetCode/index.html @@ -0,0 +1,14 @@ + + + + + Error response + + +

    Directory listing for ./static/LeetCode

    +
    + +
    + + diff --git "a/notebook/static/LeetCode/\351\235\242\350\257\225\351\242\23064.\346\261\2021+2+\342\200\246+n.html" "b/notebook/static/LeetCode/\351\235\242\350\257\225\351\242\23064.\346\261\2021+2+\342\200\246+n.html" new file mode 100644 index 0000000..657bf52 --- /dev/null +++ "b/notebook/static/LeetCode/\351\235\242\350\257\225\351\242\23064.\346\261\2021+2+\342\200\246+n.html" @@ -0,0 +1,13259 @@ + + + + +面试题64.求1+2+…+n + + + + + + + + + + + + + + + + + + + + + + +
    +
    + +
    +
    +
    In [11]:
    +
    +
    +
    # https://leetcode-cn.com/problems/qiu-12n-lcof
    +class Solution:
    +    def __init__(self):
    +        self.res = 0
    +    def sumNums(self, n: int) -> int:
    +        n > 1 and self.sumNums(n - 1)
    +        self.res += n
    +        return self.res
    +
    +s = Solution()
    +s.sumNums(3)
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[11]:
    + + + + +
    +
    6
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [5]:
    +
    +
    +
    s = Solution()
    +s.sumNums(9)
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[5]:
    + + + + +
    +
    45
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [ ]:
    +
    +
    +
    class Solution:
    +    def __init__(self):
    +        self.res = 0
    +    def sumNums(self, n: int) -> int:
    +        n > 1 and self.sumNums(n - 1)
    +        self.res += n
    +        return self.res
    +
    + +
    +
    +
    + +
    +
    +
    +
    In [13]:
    +
    +
    +
    6 << 1
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[13]:
    + + + + +
    +
    12
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [14]:
    +
    +
    +
    6 >> 1
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[14]:
    + + + + +
    +
    3
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [ ]:
    +
    +
    +
     
    +
    + +
    +
    +
    + +
    +
    +
    + + + + + + diff --git a/notebook/static/aliyun/index.html b/notebook/static/aliyun/index.html new file mode 100644 index 0000000..0fcf3a7 --- /dev/null +++ b/notebook/static/aliyun/index.html @@ -0,0 +1,14 @@ + + + + + Error response + + +

    Directory listing for ./static/aliyun

    +
    + +
    + + diff --git a/notebook/static/aliyun/oss/index.html b/notebook/static/aliyun/oss/index.html new file mode 100644 index 0000000..0b69cdf --- /dev/null +++ b/notebook/static/aliyun/oss/index.html @@ -0,0 +1,14 @@ + + + + + Error response + + +

    Directory listing for ./static/aliyun/oss

    +
    + +
    + + diff --git a/notebook/static/aliyun/oss/sts/index.html b/notebook/static/aliyun/oss/sts/index.html new file mode 100644 index 0000000..26ddcdd --- /dev/null +++ b/notebook/static/aliyun/oss/sts/index.html @@ -0,0 +1,14 @@ + + + + + Error response + + +

    Directory listing for ./static/aliyun/oss/sts

    +
    + +
    + + diff --git a/notebook/static/aliyun/oss/sts/signature.html b/notebook/static/aliyun/oss/sts/signature.html new file mode 100644 index 0000000..133dced --- /dev/null +++ b/notebook/static/aliyun/oss/sts/signature.html @@ -0,0 +1,13272 @@ + + + + +signature + + + + + + + + + + + + + + + + + + + + + + +
    +
    + +
    +
    +
    In [90]:
    +
    +
    +
    # Python Version: 3.7
    +# 计算签名
    +# https://help.aliyun.com/document_detail/28761.html?spm=a2c4g.11186623.6.791.66725328JBIR5G
    +
    +
    +#https://sts.aliyuncs.com/?&AccessKeyId=testid&SignatureMethod=HMAC-SHA1&Version=2015-04-01&Action=AssumeRole&SignatureNonce=571f8fb8-506e-11e5-8e12-b8e8563dc8d2
    +params = {
    +    "Action": "AssumeRole",
    +    "Format": "JSON",
    +    "Version": "2015-04-01",
    +    "SignatureMethod": "HMAC-SHA1",
    +    "SignatureNonce": "571f8fb8-506e-11e5-8e12-b8e8563dc8d2",
    +    "SignatureVersion": "1.0",
    +    "AccessKeyId": "testid",
    +    "Timestamp": "2015-09-01T05:57:34Z",
    +    "RoleArn": "acs:ram::1234567890123:role/firstrole",
    +    "RoleSessionName": "client"
    +}
    +
    +# 按照首字母排序
    +params_arr = list(params.items())
    +params_arr.sort(key=lambda x: x[0])
    +params_arr
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[90]:
    + + + + +
    +
    [('AccessKeyId', 'testid'),
    + ('Action', 'AssumeRole'),
    + ('Format', 'JSON'),
    + ('RoleArn', 'acs:ram::1234567890123:role/firstrole'),
    + ('RoleSessionName', 'client'),
    + ('SignatureMethod', 'HMAC-SHA1'),
    + ('SignatureNonce', '571f8fb8-506e-11e5-8e12-b8e8563dc8d2'),
    + ('SignatureVersion', '1.0'),
    + ('Timestamp', '2015-09-01T05:57:34Z'),
    + ('Version', '2015-04-01')]
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [91]:
    +
    +
    +
    from collections import OrderedDict
    +import urllib.parse
    +
    +# 拼接 sign_str
    +sign_str = "GET&%2F&"
    +sign_dict = OrderedDict(params_arr)
    +params_str = urllib.parse.urlencode(sign_dict)
    +params_str = urllib.parse.quote(params_str)
    +
    +sign_str += params_str
    +sign_str
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[91]:
    + + + + +
    +
    'GET&%2F&AccessKeyId%3Dtestid%26Action%3DAssumeRole%26Format%3DJSON%26RoleArn%3Dacs%253Aram%253A%253A1234567890123%253Arole%252Ffirstrole%26RoleSessionName%3Dclient%26SignatureMethod%3DHMAC-SHA1%26SignatureNonce%3D571f8fb8-506e-11e5-8e12-b8e8563dc8d2%26SignatureVersion%3D1.0%26Timestamp%3D2015-09-01T05%253A57%253A34Z%26Version%3D2015-04-01'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [87]:
    +
    +
    +
    # 验证签名用的 str 是否正确
    +eg = "GET&%2F&AccessKeyId%3Dtestid%26Action%3DAssumeRole%26Format%3DJSON%26RoleArn%3Dacs%253Aram%253A%253A1234567890123%253Arole%252Ffirstrole%26RoleSessionName%3Dclient%26SignatureMethod%3DHMAC-SHA1%26SignatureNonce%3D571f8fb8-506e-11e5-8e12-b8e8563dc8d2%26SignatureVersion%3D1.0%26Timestamp%3D2015-09-01T05%253A57%253A34Z%26Version%3D2015-04-01"
    +assert sign_str == eg
    +
    + +
    +
    +
    + +
    +
    +
    +
    In [82]:
    +
    +
    +
    import hashlib
    +import hmac
    +import base64
    +h = hmac.new('testsecret&'.encode('utf-8'), sign_str.encode('utf-8'), hashlib.sha1)
    +sign = h.digest()
    +sign = base64.b64encode(sign).decode()
    +sign
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[82]:
    + + + + +
    +
    'gNI7b0AyKZHxDgjBGPDgJ1Ce3L4='
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [89]:
    +
    +
    +
    # 验证签名值
    +assert sign == 'gNI7b0AyKZHxDgjBGPDgJ1Ce3L4='
    +
    + +
    +
    +
    + +
    +
    +
    +
    In [ ]:
    +
    +
    +
     
    +
    + +
    +
    +
    + +
    +
    +
    + + + + + + diff --git a/notebook/static/index.html b/notebook/static/index.html new file mode 100644 index 0000000..ae10069 --- /dev/null +++ b/notebook/static/index.html @@ -0,0 +1,14 @@ + + + + + Error response + + +

    Directory listing for ./static

    +
    + +
    + + diff --git a/notebook/static/modules/databases/connections.html b/notebook/static/modules/databases/connections.html new file mode 100644 index 0000000..0916609 --- /dev/null +++ b/notebook/static/modules/databases/connections.html @@ -0,0 +1,13272 @@ + + + + +connections + + + + + + + + + + + + + + + + + + + + + + +
    +
    + +
    +
    +
    In [6]:
    +
    +
    +
    # 您可以通过将数据库用作异步上下文管理器来控制数据库的连接/断开连接。
    +import config
    +from databases import Database
    +async with Database(config.DATABASE_URL) as database:
    +    row = await database.fetch_one(query="select * from test_user;")
    +    print(row)
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    + + +
    +
    (1, 'Daisy', 92)
    +
    +
    +
    + +
    +
    + +
    +
    +
    +
    In [ ]:
    +
    +
    +
    '''
    +If you're integrating against a web framework, 
    +then you'll probably want to hook into framework 
    +startup or shutdown events. For example, with Starlette you would use the following:
    +'''
    +@app.on_event("startup")
    +async def startup():
    +    await database.connect()
    +
    +@app.on_event("shutdown")
    +async def shutdown():
    +    await database.disconnect()
    +
    + +
    +
    +
    + +
    +
    +
    +
    In [ ]:
    +
    +
    +
    # 连接选项
    +# Use an SSL connection.
    +database = Database('postgresql://localhost/example?ssl=true')
    +
    + +
    +
    +
    + +
    +
    +
    +
    In [ ]:
    +
    +
    +
    # Use a connection pool of between 5-20 connections.
    +database = Database('mysql://localhost/example?min_size=5&max_size=20')
    +
    + +
    +
    +
    + +
    +
    +
    +
    In [ ]:
    +
    +
    +
    # 使用键值对设置选项
    +database = Database('postgresql://localhost/example', ssl=True, min_size=5, max_size=20)
    +
    + +
    +
    +
    + +
    +
    +
    +
    In [19]:
    +
    +
    +
    # 测试连接池
    +
    +from databases import Database
    +import multiprocessing as mp
    +import config
    +
    +database = Database(config.DATABASE_URL, min_size=2, max_size=5)
    +await database.connect()
    +
    +def sleep():
    +    print("hw")
    +    # res = database.fetch_one("select sleep(1)")
    +    print(res)
    +
    +pool = mp.Pool(10)
    +for i in range(10):
    +    pool.apply_async(sleep, ())
    +await database.disconnect()
    +
    + +
    +
    +
    + +
    +
    +
    +
    In [16]:
    +
    +
    +
    await database.disconnect()
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    + + +
    +
    +---------------------------------------------------------------------------
    +AssertionError                            Traceback (most recent call last)
    +<ipython-input-16-d0df956c05ea> in async-def-wrapper()
    +
    +~/.pyenv/versions/3.7.6/Python.framework/Versions/3.7/lib/python3.7/site-packages/databases/core.py in disconnect(self)
    +    103         Close all connections in the connection pool.
    +    104         """
    +--> 105         assert self.is_connected, "Already disconnected."
    +    106 
    +    107         if self._force_rollback:
    +
    +AssertionError: Already disconnected.
    +
    +
    + +
    +
    + +
    +
    +
    +
    In [ ]:
    +
    +
    +
     
    +
    + +
    +
    +
    + +
    +
    +
    + + + + + + diff --git a/notebook/static/modules/databases/index.html b/notebook/static/modules/databases/index.html new file mode 100644 index 0000000..73a7f50 --- /dev/null +++ b/notebook/static/modules/databases/index.html @@ -0,0 +1,14 @@ + + + + + Error response + + +

    Directory listing for ./static/modules/databases

    +
    + +
    + + diff --git a/notebook/static/modules/databases/quickstart.html b/notebook/static/modules/databases/quickstart.html new file mode 100644 index 0000000..2ece3b7 --- /dev/null +++ b/notebook/static/modules/databases/quickstart.html @@ -0,0 +1,13305 @@ + + + + +quickstart + + + + + + + + + + + + + + + + + + + + + + +
    +
    + +
    +
    +
    In [1]:
    +
    +
    +
    '''
    +https://www.encode.io/databases/
    +databases 为您提供了对一系列数据库的简单异步支持。
    +
    +它使您可以使用功能强大的SQLAlchemy Core表达式语言进行查询,并提供对PostgreSQL,MySQL和SQLite的支持。
    +
    +'''
    +
    +# 安装
    +!pip install databases[mysql]
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    + + +
    +
    Collecting databases[mysql]
    +  Downloading databases-0.3.2-py3-none-any.whl (18 kB)
    +Requirement already satisfied: sqlalchemy in /Users/wxnacy/.pyenv/versions/3.7.6/Python.framework/Versions/3.7/lib/python3.7/site-packages (from databases[mysql]) (1.3.15)
    +Collecting aiomysql; extra == "mysql"
    +  Downloading aiomysql-0.0.20-py3-none-any.whl (40 kB)
    +     |████████████████████████████████| 40 kB 128 kB/s ta 0:00:01
    +Requirement already satisfied: pymysql; extra == "mysql" in /Users/wxnacy/.pyenv/versions/3.7.6/Python.framework/Versions/3.7/lib/python3.7/site-packages (from databases[mysql]) (0.9.3)
    +ERROR: aiomysql 0.0.20 has requirement PyMySQL<=0.9.2,>=0.9, but you'll have pymysql 0.9.3 which is incompatible.
    +Installing collected packages: aiomysql, databases
    +Successfully installed aiomysql-0.0.20 databases-0.3.2
    +
    +
    +
    + +
    +
    + +
    +
    +
    +
    In [12]:
    +
    +
    +
    # Create a database instance, and connect to it.
    +from databases import Database
    +import config
    +database = Database(config.DATABASE_URL)
    +await database.connect()
    +
    + +
    +
    +
    + +
    +
    +
    +
    In [13]:
    +
    +
    +
    # Create a table.
    +query = """
    +DROP TABLE IF EXISTS `test_user`;
    +CREATE TABLE `test_user` (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100), score INT);
    +"""
    +await database.execute(query=query)
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[13]:
    + + + + +
    +
    0
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [14]:
    +
    +
    +
    # Insert some data.
    +query = "INSERT INTO test_user (name, score) VALUES (:name, :score)"
    +values = [
    +    {"name": "Daisy", "score": 92},
    +    {"name": "Neil", "score": 87},
    +    {"name": "Carol", "score": 43},
    +]
    +await database.execute_many(query=query, values=values)
    +
    + +
    +
    +
    + +
    +
    +
    +
    In [15]:
    +
    +
    +
    # Run a database query.
    +query = "SELECT * FROM test_user"
    +await database.fetch_all(query=query)
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[15]:
    + + + + +
    +
    [(1, 'Daisy', 92), (2, 'Neil', 87), (3, 'Carol', 43)]
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [16]:
    +
    +
    +
    # Fetch single row
    +query = "SELECT * FROM test_user WHERE id = :id"
    +await database.fetch_one(query=query, values={"id": 1})
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[16]:
    + + + + +
    +
    (1, 'Daisy', 92)
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [10]:
    +
    +
    +
    # Close all connection in the connection pool
    +await database.disconnect()
    +
    + +
    +
    +
    + +
    +
    +
    +
    In [ ]:
    +
    +
    +
     
    +
    + +
    +
    +
    + +
    +
    +
    + + + + + + diff --git a/notebook/static/modules/databases/sqlalchemy.html b/notebook/static/modules/databases/sqlalchemy.html new file mode 100644 index 0000000..e8e7c45 --- /dev/null +++ b/notebook/static/modules/databases/sqlalchemy.html @@ -0,0 +1,13425 @@ + + + + +sqlalchemy + + + + + + + + + + + + + + + + + + + + + + +
    +
    + +
    +
    +
    In [5]:
    +
    +
    +
    # 使用 sqlalchemy 进行数据库操作
    +import sqlalchemy
    +from sqlalchemy import Column
    +from sqlalchemy import Integer
    +from sqlalchemy import String
    +from sqlalchemy import Boolean
    +import config
    +
    +
    +metadata = sqlalchemy.MetaData()
    +
    +user = sqlalchemy.Table(
    +    "test_user",
    +    metadata,
    +    Column("id", Integer, primary_key=True),
    +    Column("name", String(length=100)),
    +    Column("score", Integer),
    +)
    +
    + +
    +
    +
    + +
    +
    +
    +
    In [3]:
    +
    +
    +
    # Create a database instance, and connect to it.
    +from databases import Database
    +database = Database(config.DATABASE_URL)
    +await database.connect()
    +
    + +
    +
    +
    + +
    +
    +
    +
    In [4]:
    +
    +
    +
    # Execute
    +query = user.insert()
    +values = {"name": "wxnacy", "score": 100}
    +await database.execute(query=query, values=values)
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[4]:
    + + + + +
    +
    7
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [15]:
    +
    +
    +
    # Execute many
    +query = user.insert()
    +values = [
    +    {"name": "wxnacy", "score": 100},
    +    {"name": "wen", "score": 100}
    +]
    +await database.execute_many(query=query, values=values)
    +
    + +
    +
    +
    + +
    +
    +
    +
    In [27]:
    +
    +
    +
    # Fetch multiple rows
    +query = user.select()
    +rows = await database.fetch_all(query=query)
    +rows, type(rows)
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[27]:
    + + + + +
    +
    ([(1, 'Daisy', 92),
    +  (2, 'Neil', 87),
    +  (3, 'Carol', 43),
    +  (4, 'wxnacy', 100),
    +  (5, 'wxnacy', 100),
    +  (6, 'wen', 100)],
    + list)
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [29]:
    +
    +
    +
    # Fetch single row
    +query = user.select()
    +row = await database.fetch_one(query=query)
    +row, type(row), row.id
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[29]:
    + + + + +
    +
    ((1, 'Daisy', 92), sqlalchemy.engine.result.RowProxy, 1)
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [20]:
    +
    +
    +
    # Fetch single value, defaults to `column=0`.
    +query = user.select()
    +await database.fetch_val(query=query)
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[20]:
    + + + + +
    +
    1
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [22]:
    +
    +
    +
    # Fetch multiple rows without loading them all into memory at once
    +query = user.select()
    +async for row in database.iterate(query=query):
    +    print(row)
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    + + +
    +
    (1, 'Daisy', 92)
    +(2, 'Neil', 87)
    +(3, 'Carol', 43)
    +(4, 'wxnacy', 100)
    +(5, 'wxnacy', 100)
    +(6, 'wen', 100)
    +
    +
    +
    + +
    +
    + +
    +
    +
    +
    In [7]:
    +
    +
    +
    help(user.select)
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    + + +
    +
    Help on method select in module sqlalchemy.sql.selectable:
    +
    +select(whereclause=None, **params) method of sqlalchemy.sql.schema.Table instance
    +    return a SELECT of this :class:`.FromClause`.
    +    
    +    .. seealso::
    +    
    +        :func:`~.sql.expression.select` - general purpose
    +        method which allows for arbitrary column lists.
    +
    +
    +
    +
    + +
    +
    + +
    +
    +
    +
    In [10]:
    +
    +
    +
    from sqlalchemy import text
    +query = user.select(text("id = 1"))
    +await database.fetch_all(query=query)
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[10]:
    + + + + +
    +
    [(1, 'Daisy', 92)]
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [ ]:
    +
    +
    +
     
    +
    + +
    +
    +
    + +
    +
    +
    + + + + + + diff --git a/notebook/static/modules/faker/command.html b/notebook/static/modules/faker/command.html new file mode 100644 index 0000000..8ea878e --- /dev/null +++ b/notebook/static/modules/faker/command.html @@ -0,0 +1,13278 @@ + + + + +command + + + + + + + + + + + + + + + + + + + + + + +
    +
    + +
    +
    +
    In [1]:
    +
    +
    +
    # 生成名字
    +!faker name
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    + + +
    +
    Chris Curtis
    +
    +
    +
    +
    + +
    +
    + +
    +
    +
    +
    In [2]:
    +
    +
    +
    # 设置语言
    +!faker -l zh_CN name
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    + + +
    +
    窦鑫
    +
    +
    +
    +
    + +
    +
    + +
    +
    +
    +
    In [4]:
    +
    +
    +
    # 重复三次,并设置后缀
    +!faker -r 3 -s ";" name
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    + + +
    +
    Victor Martinez;
    +Brenda Johnson;
    +Timothy Bridges;
    +
    +
    +
    + +
    +
    + +
    +
    +
    +
    In [5]:
    +
    +
    +
    # 帮助文档
    +!faker -h
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    + + +
    +
    usage: faker [-h] [--version] [-v] [-o output] [-l LOCALE] [-r REPEAT]
    +             [-s SEP] [--seed SEED] [-i [INCLUDE [INCLUDE ...]]]
    +             [fake] [fake argument [fake argument ...]]
    +
    +faker version 4.1.0
    +
    +positional arguments:
    +  fake                  name of the fake to generate output for (e.g. profile)
    +  fake argument         optional arguments to pass to the fake (e.g. the
    +                        profile fake takes an optional list of comma separated
    +                        field names as the first argument)
    +
    +optional arguments:
    +  -h, --help            show this help message and exit
    +  --version             show program's version number and exit
    +  -v, --verbose         show INFO logging events instead of CRITICAL, which is
    +                        the default. These logging events provide insight into
    +                        localization of specific providers.
    +  -o output             redirect output to a file
    +  -l LOCALE, --lang LOCALE
    +                        specify the language for a localized provider (e.g.
    +                        de_DE)
    +  -r REPEAT, --repeat REPEAT
    +                        generate the specified number of outputs
    +  -s SEP, --sep SEP     use the specified separator after each output
    +  --seed SEED           specify a seed for the random generator so that
    +                        results are repeatable. Also compatible with 'repeat'
    +                        option
    +  -i [INCLUDE [INCLUDE ...]], --include [INCLUDE [INCLUDE ...]]
    +                        list of additional custom providers to user, given as
    +                        the import path of the module containing your Provider
    +                        class (not the provider class itself)
    +
    +supported locales:
    +
    +  ar_AA, ar_EG, ar_JO, ar_PS, ar_SA, bg_BG, bs_BA, cs_CZ, de, de_AT, de_CH, de_DE, dk_DK, el_CY, el_GR, en, en_AU, en_CA, en_GB, en_IE, en_IN, en_NZ, en_PH, en_TH, en_US, es, es_CA, es_ES, es_MX, et_EE, fa_IR, fi_FI, fil_PH, fr_CH, fr_FR, fr_QC, he_IL, hi_IN, hr_HR, hu_HU, hy_AM, id_ID, it_IT, ja_JP, ka_GE, ko_KR, la, lb_LU, lt_LT, lv_LV, mt_MT, ne_NP, nl_BE, nl_NL, no_NO, pl_PL, pt_BR, pt_PT, ro_RO, ru_RU, sk_SK, sl_SI, sv_SE, ta_IN, th_TH, tl_PH, tr_TR, tw_GH, uk_UA, zh_CN, zh_TW
    +
    +  Faker can take a locale as an optional argument, to return localized data. If
    +  no locale argument is specified, the factory falls back to the user's OS
    +  locale as long as it is supported by at least one of the providers.
    +     - for this user, the default locale is en_US.
    +
    +  If the optional argument locale and/or user's default locale is not available
    +  for the specified provider, the factory falls back to faker's default locale,
    +  which is en_US.
    +
    +examples:
    +
    +  $ faker address
    +  968 Bahringer Garden Apt. 722
    +  Kristinaland, NJ 09890
    +
    +  $ faker -l de_DE address
    +  Samira-Niemeier-Allee 56
    +  94812 Biedenkopf
    +
    +  $ faker profile ssn,birthdate
    +  {'ssn': u'628-10-1085', 'birthdate': '2008-03-29'}
    +
    +  $ faker -r=3 -s=";" name
    +  Willam Kertzmann;
    +  Josiah Maggio;
    +  Gayla Schmitt;
    +
    +
    +
    + +
    +
    + +
    +
    +
    + + + + + + diff --git a/notebook/static/modules/faker/index.html b/notebook/static/modules/faker/index.html new file mode 100644 index 0000000..bfaabc4 --- /dev/null +++ b/notebook/static/modules/faker/index.html @@ -0,0 +1,14 @@ + + + + + Error response + + +

    Directory listing for ./static/modules/faker

    +
    + +
    + + diff --git a/notebook/static/modules/faker/locales.html b/notebook/static/modules/faker/locales.html new file mode 100644 index 0000000..826f03b --- /dev/null +++ b/notebook/static/modules/faker/locales.html @@ -0,0 +1,13266 @@ + + + + +locales + + + + + + + + + + + + + + + + + + + + + + +
    +
    + +
    +
    +
    In [1]:
    +
    +
    +
    from faker import Faker
    +
    +# 设置中文
    +fake = Faker('zh_CN')
    +fake.name()
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[1]:
    + + + + +
    +
    '李颖'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [3]:
    +
    +
    +
    # 设置多个语言
    +fake = Faker(['zh_CN', 'ja_JP', 'en_US'])
    +for _ in range(10):
    +    print(fake.name())
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    + + +
    +
    叶柳
    +田中 充
    +Thomas Decker
    +Kyle Avila
    +何勇
    +田辺 結衣
    +Ashley Gonzalez
    +梁莹
    +Cheryl Long
    +大垣 治
    +
    +
    +
    + +
    +
    + +
    +
    +
    +
    In [5]:
    +
    +
    +
    # 设置多语言的权重
    +
    +from collections import OrderedDict
    +locales = OrderedDict([
    +    ('en-US', 1),
    +    ('en-PH', 2),
    +    ('ja_JP', 3),
    +])
    +fake = Faker(locales)
    +
    + +
    +
    +
    + +
    +
    +
    +
    In [6]:
    +
    +
    +
    # 获取指定的语言列表
    +fake.locales
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[6]:
    + + + + +
    +
    ['en_US', 'en_PH', 'ja_JP']
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [7]:
    +
    +
    +
    # 单独获取列表中某一个语言
    +fake['en-US'].name()
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[7]:
    + + + + +
    +
    'Nicholas Malone'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [ ]:
    +
    +
    +
     
    +
    + +
    +
    +
    + +
    +
    +
    + + + + + + diff --git a/notebook/static/modules/faker/quickstart.html b/notebook/static/modules/faker/quickstart.html new file mode 100644 index 0000000..f0c41d8 --- /dev/null +++ b/notebook/static/modules/faker/quickstart.html @@ -0,0 +1,13657 @@ + + + + +quickstart + + + + + + + + + + + + + + + + + + + + + + +
    +
    + +
    +
    +
    In [1]:
    +
    +
    +
    #!/usr/bin/env python
    +# -*- coding:utf-8 -*-
    +# Author: wxnacy(wxnacy@gmail.com)
    +# Faker 快速开始
    +# https://github.com/joke2k/faker
    +
    +from faker import Faker
    +fake = Faker()
    +# 生成名字
    +fake.name()
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[1]:
    + + + + +
    +
    'Michelle Little'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [4]:
    +
    +
    +
    # 生成地址
    +fake.address()
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[4]:
    + + + + +
    +
    '0226 Donald Courts\nColeborough, WA 70527'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [5]:
    +
    +
    +
    # 生成文本
    +fake.text()
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[5]:
    + + + + +
    +
    'Age political even bad coach success. List through hand.\nDo animal with office born source. Base agree feeling human hour. Answer player structure glass.\nBusiness issue plant box.'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [6]:
    +
    +
    +
    # 生成 user_agent
    +fake.user_agent()
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[6]:
    + + + + +
    +
    'Opera/9.26.(X11; Linux x86_64; om-KE) Presto/2.9.171 Version/11.00'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [2]:
    +
    +
    +
    # 生成 ipv4
    +fake.ipv4()
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[2]:
    + + + + +
    +
    '1.182.26.39'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [3]:
    +
    +
    +
    # 生成 android 平台名称
    +fake.android_platform_token()
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[3]:
    + + + + +
    +
    'Android 2.2.2'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [4]:
    +
    +
    +
    # 生成 ios 平台名称
    +fake.ios_platform_token()
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[4]:
    + + + + +
    +
    'iPad; CPU iPad OS 7_1_2 like Mac OS X'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [7]:
    +
    +
    +
    # 查看 faker 属性
    +dir(fake)
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[7]:
    + + + + +
    +
    ['__class__',
    + '__delattr__',
    + '__dict__',
    + '__dir__',
    + '__doc__',
    + '__eq__',
    + '__format__',
    + '__ge__',
    + '__getattr__',
    + '__getattribute__',
    + '__getitem__',
    + '__gt__',
    + '__hash__',
    + '__init__',
    + '__init_subclass__',
    + '__le__',
    + '__lt__',
    + '__module__',
    + '__ne__',
    + '__new__',
    + '__reduce__',
    + '__reduce_ex__',
    + '__repr__',
    + '__setattr__',
    + '__sizeof__',
    + '__str__',
    + '__subclasshook__',
    + '__weakref__',
    + '_factories',
    + '_factory_map',
    + '_locales',
    + '_map_provider_method',
    + '_select_factory',
    + '_weights',
    + 'add_provider',
    + 'address',
    + 'am_pm',
    + 'android_platform_token',
    + 'ascii_company_email',
    + 'ascii_email',
    + 'ascii_free_email',
    + 'ascii_safe_email',
    + 'bank_country',
    + 'bban',
    + 'binary',
    + 'boolean',
    + 'bothify',
    + 'bs',
    + 'building_number',
    + 'cache_pattern',
    + 'catch_phrase',
    + 'century',
    + 'chrome',
    + 'city',
    + 'city_prefix',
    + 'city_suffix',
    + 'color',
    + 'color_name',
    + 'company',
    + 'company_email',
    + 'company_suffix',
    + 'coordinate',
    + 'country',
    + 'country_calling_code',
    + 'country_code',
    + 'credit_card_expire',
    + 'credit_card_full',
    + 'credit_card_number',
    + 'credit_card_provider',
    + 'credit_card_security_code',
    + 'cryptocurrency',
    + 'cryptocurrency_code',
    + 'cryptocurrency_name',
    + 'csv',
    + 'currency',
    + 'currency_code',
    + 'currency_name',
    + 'currency_symbol',
    + 'date',
    + 'date_between',
    + 'date_between_dates',
    + 'date_object',
    + 'date_of_birth',
    + 'date_this_century',
    + 'date_this_decade',
    + 'date_this_month',
    + 'date_this_year',
    + 'date_time',
    + 'date_time_ad',
    + 'date_time_between',
    + 'date_time_between_dates',
    + 'date_time_this_century',
    + 'date_time_this_decade',
    + 'date_time_this_month',
    + 'date_time_this_year',
    + 'day_of_month',
    + 'day_of_week',
    + 'dga',
    + 'domain_name',
    + 'domain_word',
    + 'dsv',
    + 'ean',
    + 'ean13',
    + 'ean8',
    + 'ein',
    + 'email',
    + 'factories',
    + 'file_extension',
    + 'file_name',
    + 'file_path',
    + 'firefox',
    + 'first_name',
    + 'first_name_female',
    + 'first_name_male',
    + 'format',
    + 'free_email',
    + 'free_email_domain',
    + 'future_date',
    + 'future_datetime',
    + 'generator_attrs',
    + 'get_formatter',
    + 'get_providers',
    + 'hex_color',
    + 'hexify',
    + 'hostname',
    + 'http_method',
    + 'iban',
    + 'image_url',
    + 'internet_explorer',
    + 'invalid_ssn',
    + 'ios_platform_token',
    + 'ipv4',
    + 'ipv4_network_class',
    + 'ipv4_private',
    + 'ipv4_public',
    + 'ipv6',
    + 'isbn10',
    + 'isbn13',
    + 'iso8601',
    + 'items',
    + 'itin',
    + 'job',
    + 'language_code',
    + 'last_name',
    + 'last_name_female',
    + 'last_name_male',
    + 'latitude',
    + 'latlng',
    + 'lexify',
    + 'license_plate',
    + 'linux_platform_token',
    + 'linux_processor',
    + 'local_latlng',
    + 'locale',
    + 'locales',
    + 'location_on_land',
    + 'longitude',
    + 'mac_address',
    + 'mac_platform_token',
    + 'mac_processor',
    + 'md5',
    + 'military_apo',
    + 'military_dpo',
    + 'military_ship',
    + 'military_state',
    + 'mime_type',
    + 'month',
    + 'month_name',
    + 'msisdn',
    + 'name',
    + 'name_female',
    + 'name_male',
    + 'null_boolean',
    + 'numerify',
    + 'opera',
    + 'paragraph',
    + 'paragraphs',
    + 'parse',
    + 'password',
    + 'past_date',
    + 'past_datetime',
    + 'phone_number',
    + 'port_number',
    + 'postalcode',
    + 'postalcode_in_state',
    + 'postalcode_plus4',
    + 'postcode',
    + 'postcode_in_state',
    + 'prefix',
    + 'prefix_female',
    + 'prefix_male',
    + 'profile',
    + 'provider',
    + 'providers',
    + 'psv',
    + 'pybool',
    + 'pydecimal',
    + 'pydict',
    + 'pyfloat',
    + 'pyint',
    + 'pyiterable',
    + 'pylist',
    + 'pyset',
    + 'pystr',
    + 'pystr_format',
    + 'pystruct',
    + 'pytuple',
    + 'random',
    + 'random_choices',
    + 'random_digit',
    + 'random_digit_not_null',
    + 'random_digit_not_null_or_empty',
    + 'random_digit_or_empty',
    + 'random_element',
    + 'random_elements',
    + 'random_int',
    + 'random_letter',
    + 'random_letters',
    + 'random_lowercase_letter',
    + 'random_number',
    + 'random_sample',
    + 'random_uppercase_letter',
    + 'randomize_nb_elements',
    + 'rgb_color',
    + 'rgb_css_color',
    + 'safari',
    + 'safe_color_name',
    + 'safe_email',
    + 'safe_hex_color',
    + 'secondary_address',
    + 'seed',
    + 'seed_instance',
    + 'seed_locale',
    + 'sentence',
    + 'sentences',
    + 'set_formatter',
    + 'sha1',
    + 'sha256',
    + 'simple_profile',
    + 'slug',
    + 'ssn',
    + 'state',
    + 'state_abbr',
    + 'street_address',
    + 'street_name',
    + 'street_suffix',
    + 'suffix',
    + 'suffix_female',
    + 'suffix_male',
    + 'tar',
    + 'text',
    + 'texts',
    + 'time',
    + 'time_delta',
    + 'time_object',
    + 'time_series',
    + 'timezone',
    + 'tld',
    + 'tsv',
    + 'unix_device',
    + 'unix_partition',
    + 'unix_time',
    + 'upc_a',
    + 'upc_e',
    + 'uri',
    + 'uri_extension',
    + 'uri_page',
    + 'uri_path',
    + 'url',
    + 'user_agent',
    + 'user_name',
    + 'uuid4',
    + 'weights',
    + 'windows_platform_token',
    + 'word',
    + 'words',
    + 'year',
    + 'zip',
    + 'zipcode',
    + 'zipcode_in_state',
    + 'zipcode_plus4']
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [ ]:
    +
    +
    +
     
    +
    + +
    +
    +
    + +
    +
    +
    + + + + + + diff --git a/notebook/static/modules/fastapi/Untitled.html b/notebook/static/modules/fastapi/Untitled.html new file mode 100644 index 0000000..8753b26 --- /dev/null +++ b/notebook/static/modules/fastapi/Untitled.html @@ -0,0 +1,13128 @@ + + + + +Untitled + + + + + + + + + + + + + + + + + + + + + + +
    +
    + +
    +
    +
    In [1]:
    +
    +
    +
    !pip show databases
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    + + +
    +
    WARNING: Package(s) not found: databases
    +
    +
    +
    + +
    +
    + +
    +
    +
    +
    In [ ]:
    +
    +
    +
     
    +
    + +
    +
    +
    + +
    +
    +
    + + + + + + diff --git a/notebook/static/modules/fastapi/index.html b/notebook/static/modules/fastapi/index.html new file mode 100644 index 0000000..5d01453 --- /dev/null +++ b/notebook/static/modules/fastapi/index.html @@ -0,0 +1,14 @@ + + + + + Error response + + +

    Directory listing for ./static/modules/fastapi

    +
    + +
    + + diff --git a/notebook/static/modules/hashlib.html b/notebook/static/modules/hashlib.html new file mode 100644 index 0000000..fdc1d8a --- /dev/null +++ b/notebook/static/modules/hashlib.html @@ -0,0 +1,13325 @@ + + + + +hashlib + + + + + + + + + + + + + + + + + + + + + + +
    +
    + +
    +
    +
    In [3]:
    +
    +
    +
    import hashlib
    +
    +# 计算字符串 md5 值
    +message = "wxnacy"
    +md5 = hashlib.md5()
    +md5.update(message.encode('utf-8'))
    +md5.hexdigest()
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[3]:
    + + + + +
    +
    '1f806eb48b670c40af49a3f764ba086f'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [4]:
    +
    +
    +
    # 计算字符串 md5 值
    +hashlib.md5(message.encode('utf-8')).hexdigest()
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[4]:
    + + + + +
    +
    '1f806eb48b670c40af49a3f764ba086f'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [6]:
    +
    +
    +
    # 计算文件 md5 值
    +def encrypt_file(filename):
    +    hash_md5 = hashlib.md5()
    +    with open(filename, "rb") as f:
    +        for chunk in iter(lambda: f.read(4096), b""):
    +            hash_md5.update(chunk)
    +    return hash_md5.hexdigest()
    +
    +encrypt_file('./hashlib.ipynb')
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[6]:
    + + + + +
    +
    'f0629f11bf649b31f040ec4248818cba'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [9]:
    +
    +
    +
    # 计算网址内容的 md5 值
    +from urllib.request import urlopen
    +
    +def encrypt_url(url, max_file_size=100 * 1024 * 1024):
    +    remote = urlopen(url)
    +    h = hashlib.md5()
    +
    +    total_read = 0
    +    while True:
    +        data = remote.read(4096)
    +        total_read += 4096
    +
    +        if not data or total_read > max_file_size:
    +            break
    +        h.update(data)
    +    return h.hexdigest()
    +
    +encrypt_url("https://wxnacy.com")
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[9]:
    + + + + +
    +
    '7943484aaae8ec07556c23a41be3cdbe'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [2]:
    +
    +
    +
    # 查看其它 hash 值算法
    +dir(hashlib)
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[2]:
    + + + + +
    +
    ['__all__',
    + '__builtin_constructor_cache',
    + '__builtins__',
    + '__cached__',
    + '__doc__',
    + '__file__',
    + '__get_builtin_constructor',
    + '__loader__',
    + '__name__',
    + '__package__',
    + '__spec__',
    + '_hashlib',
    + 'algorithms_available',
    + 'algorithms_guaranteed',
    + 'blake2b',
    + 'blake2s',
    + 'md5',
    + 'new',
    + 'pbkdf2_hmac',
    + 'scrypt',
    + 'sha1',
    + 'sha224',
    + 'sha256',
    + 'sha384',
    + 'sha3_224',
    + 'sha3_256',
    + 'sha3_384',
    + 'sha3_512',
    + 'sha512',
    + 'shake_128',
    + 'shake_256']
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [ ]:
    +
    +
    +
     
    +
    + +
    +
    +
    + +
    +
    +
    + + + + + + diff --git a/notebook/static/modules/hmac.html b/notebook/static/modules/hmac.html new file mode 100644 index 0000000..6445b5f --- /dev/null +++ b/notebook/static/modules/hmac.html @@ -0,0 +1,13174 @@ + + + + +hmac + + + + + + + + + + + + + + + + + + + + + + +
    +
    + +
    +
    +
    In [8]:
    +
    +
    +
    # 计算 hmac_sha1 加密值
    +import hmac
    +import hashlib
    +
    +message = 'wxnacy'
    +key = 'wxnacy.com'
    +
    +h = hmac.new(key.encode('utf-8'), message.encode('utf-8'), hashlib.sha1)
    +# 计算 16 进制编码字符串
    +h.hexdigest()
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[8]:
    + + + + +
    +
    '56c83dc453f66c4d4bf05f8ab65179f12b7410e2'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [7]:
    +
    +
    +
    # 使用 base64 将 hmac 结果编码为字符串
    +import base64
    +base64.b64encode(h.digest()).decode()
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[7]:
    + + + + +
    +
    'Vsg9xFP2bE1L8F+KtlF58St0EOI='
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [ ]:
    +
    +
    +
     
    +
    + +
    +
    +
    + +
    +
    +
    + + + + + + diff --git a/notebook/static/modules/index.html b/notebook/static/modules/index.html new file mode 100644 index 0000000..e91d1f6 --- /dev/null +++ b/notebook/static/modules/index.html @@ -0,0 +1,14 @@ + + + + + Error response + + +

    Directory listing for ./static/modules

    +
    + +
    + + diff --git a/notebook/static/modules/sqlalchemy/Untitled.html b/notebook/static/modules/sqlalchemy/Untitled.html new file mode 100644 index 0000000..acf7f2c --- /dev/null +++ b/notebook/static/modules/sqlalchemy/Untitled.html @@ -0,0 +1,13486 @@ + + + + +Untitled + + + + + + + + + + + + + + + + + + + + + + +
    +
    + +
    +
    +
    In [9]:
    +
    +
    +
    !pip install mysqlclient
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    + + +
    +
    Collecting mysqlclient
    +  Using cached mysqlclient-1.4.6.tar.gz (85 kB)
    +Using legacy setup.py install for mysqlclient, since package 'wheel' is not installed.
    +Installing collected packages: mysqlclient
    +    Running setup.py install for mysqlclient ... error
    +    ERROR: Command errored out with exit status 1:
    +     command: /Users/wxnacy/.pyenv/versions/3.7.6/envs/notebook/bin/python3.7 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/kz/8syfctw919zdt3shr9w5j8v00000gn/T/pip-install-gzuju0kh/mysqlclient/setup.py'"'"'; __file__='"'"'/private/var/folders/kz/8syfctw919zdt3shr9w5j8v00000gn/T/pip-install-gzuju0kh/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/kz/8syfctw919zdt3shr9w5j8v00000gn/T/pip-record-7d7ia_ag/install-record.txt --single-version-externally-managed --compile --install-headers /Users/wxnacy/.pyenv/versions/3.7.6/envs/notebook/include/site/python3.7/mysqlclient
    +         cwd: /private/var/folders/kz/8syfctw919zdt3shr9w5j8v00000gn/T/pip-install-gzuju0kh/mysqlclient/
    +    Complete output (30 lines):
    +    running install
    +    running build
    +    running build_py
    +    creating build
    +    creating build/lib.macosx-10.14-x86_64-3.7
    +    creating build/lib.macosx-10.14-x86_64-3.7/MySQLdb
    +    copying MySQLdb/__init__.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb
    +    copying MySQLdb/_exceptions.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb
    +    copying MySQLdb/compat.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb
    +    copying MySQLdb/connections.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb
    +    copying MySQLdb/converters.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb
    +    copying MySQLdb/cursors.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb
    +    copying MySQLdb/release.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb
    +    copying MySQLdb/times.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb
    +    creating build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants
    +    copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants
    +    copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants
    +    copying MySQLdb/constants/CR.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants
    +    copying MySQLdb/constants/ER.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants
    +    copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants
    +    copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants
    +    running build_ext
    +    building 'MySQLdb._mysql' extension
    +    creating build/temp.macosx-10.14-x86_64-3.7
    +    creating build/temp.macosx-10.14-x86_64-3.7/MySQLdb
    +    clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -Dversion_info=(1,4,6,'final',0) -D__version__=1.4.6 -I/usr/local/Cellar/mysql/5.7.19/include/mysql -I/Users/wxnacy/.pyenv/versions/3.7.6/envs/notebook/include -I/Users/wxnacy/.pyenv/versions/3.7.6/Python.framework/Versions/3.7/include/python3.7m -c MySQLdb/_mysql.c -o build/temp.macosx-10.14-x86_64-3.7/MySQLdb/_mysql.o
    +    clang -bundle -undefined dynamic_lookup -L/usr/local/opt/readline/lib -L/usr/local/opt/readline/lib -L/Users/wxnacy/.pyenv/versions/3.7.6/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/readline/lib -L/Users/wxnacy/.pyenv/versions/3.7.6/lib build/temp.macosx-10.14-x86_64-3.7/MySQLdb/_mysql.o -L/usr/local/Cellar/mysql/5.7.19/lib -lmysqlclient -lssl -lcrypto -o build/lib.macosx-10.14-x86_64-3.7/MySQLdb/_mysql.cpython-37m-darwin.so
    +    ld: library not found for -lssl
    +    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    +    error: command 'clang' failed with exit status 1
    +    ----------------------------------------
    +ERROR: Command errored out with exit status 1: /Users/wxnacy/.pyenv/versions/3.7.6/envs/notebook/bin/python3.7 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/kz/8syfctw919zdt3shr9w5j8v00000gn/T/pip-install-gzuju0kh/mysqlclient/setup.py'"'"'; __file__='"'"'/private/var/folders/kz/8syfctw919zdt3shr9w5j8v00000gn/T/pip-install-gzuju0kh/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/kz/8syfctw919zdt3shr9w5j8v00000gn/T/pip-record-7d7ia_ag/install-record.txt --single-version-externally-managed --compile --install-headers /Users/wxnacy/.pyenv/versions/3.7.6/envs/notebook/include/site/python3.7/mysqlclient Check the logs for full command output.
    +
    +
    +
    + +
    +
    + +
    +
    +
    +
    In [5]:
    +
    +
    +
    # pip install sqlalchemy pymysql 
    +from sqlalchemy import create_engine
    +from sqlalchemy.ext.declarative import declarative_base
    +from sqlalchemy.orm import sessionmaker
    +import config
    +
    +SQLALCHEMY_DATABASE_URL = config.DATABASE_URL
    +
    +engine = create_engine(
    +    SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
    +)
    +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
    +
    +Base = declarative_base()
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    + + +
    +
    +---------------------------------------------------------------------------
    +ModuleNotFoundError                       Traceback (most recent call last)
    +<ipython-input-5-173d78124eaf> in <module>
    +      8 
    +      9 engine = create_engine(
    +---> 10     SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
    +     11 )
    +     12 SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
    +
    +~/.pyenv/versions/3.7.6/envs/notebook/lib/python3.7/site-packages/sqlalchemy/engine/__init__.py in create_engine(*args, **kwargs)
    +    486     strategy = kwargs.pop("strategy", default_strategy)
    +    487     strategy = strategies.strategies[strategy]
    +--> 488     return strategy.create(*args, **kwargs)
    +    489 
    +    490 
    +
    +~/.pyenv/versions/3.7.6/envs/notebook/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py in create(self, name_or_url, **kwargs)
    +     85                 if k in kwargs:
    +     86                     dbapi_args[k] = pop_kwarg(k)
    +---> 87             dbapi = dialect_cls.dbapi(**dbapi_args)
    +     88 
    +     89         dialect_args["dbapi"] = dbapi
    +
    +~/.pyenv/versions/3.7.6/envs/notebook/lib/python3.7/site-packages/sqlalchemy/dialects/mysql/mysqldb.py in dbapi(cls)
    +    116     @classmethod
    +    117     def dbapi(cls):
    +--> 118         return __import__("MySQLdb")
    +    119 
    +    120     def on_connect(self):
    +
    +ModuleNotFoundError: No module named 'MySQLdb'
    +
    +
    + +
    +
    + +
    +
    +
    +
    In [ ]:
    +
    +
    +
    from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
    +
    +class User(Base):
    +    __tablename__ = 'test_user'
    +    id = Column(Integer, primary_key = True)
    +    name = Column(String, default="")
    +    score = Column(Integer, default=0)
    +
    + +
    +
    +
    + +
    +
    +
    +
    In [12]:
    +
    +
    +
    import sqlalchemy
    +for module in sqlalchemy, sqlalchemy.orm:
    +    print(module.__all__)
    +    for key in module.__all__:
    +        print(key)
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    + + +
    +
    ['ARRAY', 'BIGINT', 'BINARY', 'BLANK_SCHEMA', 'BLOB', 'BOOLEAN', 'BigInteger', 'Binary', 'Boolean', 'CHAR', 'CLOB', 'CheckConstraint', 'Column', 'ColumnDefault', 'Computed', 'Constraint', 'DATE', 'DATETIME', 'DDL', 'DECIMAL', 'Date', 'DateTime', 'DefaultClause', 'Enum', 'FLOAT', 'FetchedValue', 'Float', 'ForeignKey', 'ForeignKeyConstraint', 'INT', 'INTEGER', 'Index', 'Integer', 'Interval', 'JSON', 'LargeBinary', 'MetaData', 'NCHAR', 'NUMERIC', 'NVARCHAR', 'Numeric', 'PassiveDefault', 'PickleType', 'PrimaryKeyConstraint', 'REAL', 'SMALLINT', 'Sequence', 'SmallInteger', 'String', 'TEXT', 'TIME', 'TIMESTAMP', 'Table', 'Text', 'ThreadLocalMetaData', 'Time', 'TypeDecorator', 'Unicode', 'UnicodeText', 'UniqueConstraint', 'VARBINARY', 'VARCHAR', 'alias', 'all_', 'and_', 'any_', 'asc', 'between', 'bindparam', 'case', 'cast', 'collate', 'column', 'create_engine', 'delete', 'desc', 'distinct', 'engine_from_config', 'except_', 'except_all', 'exists', 'extract', 'false', 'func', 'funcfilter', 'insert', 'inspect', 'intersect', 'intersect_all', 'join', 'lateral', 'literal', 'literal_column', 'modifier', 'not_', 'null', 'nullsfirst', 'nullslast', 'or_', 'outerjoin', 'outparam', 'over', 'select', 'subquery', 'table', 'tablesample', 'text', 'true', 'tuple_', 'type_coerce', 'union', 'union_all', 'update', 'within_group']
    +ARRAY
    +BIGINT
    +BINARY
    +BLANK_SCHEMA
    +BLOB
    +BOOLEAN
    +BigInteger
    +Binary
    +Boolean
    +CHAR
    +CLOB
    +CheckConstraint
    +Column
    +ColumnDefault
    +Computed
    +Constraint
    +DATE
    +DATETIME
    +DDL
    +DECIMAL
    +Date
    +DateTime
    +DefaultClause
    +Enum
    +FLOAT
    +FetchedValue
    +Float
    +ForeignKey
    +ForeignKeyConstraint
    +INT
    +INTEGER
    +Index
    +Integer
    +Interval
    +JSON
    +LargeBinary
    +MetaData
    +NCHAR
    +NUMERIC
    +NVARCHAR
    +Numeric
    +PassiveDefault
    +PickleType
    +PrimaryKeyConstraint
    +REAL
    +SMALLINT
    +Sequence
    +SmallInteger
    +String
    +TEXT
    +TIME
    +TIMESTAMP
    +Table
    +Text
    +ThreadLocalMetaData
    +Time
    +TypeDecorator
    +Unicode
    +UnicodeText
    +UniqueConstraint
    +VARBINARY
    +VARCHAR
    +alias
    +all_
    +and_
    +any_
    +asc
    +between
    +bindparam
    +case
    +cast
    +collate
    +column
    +create_engine
    +delete
    +desc
    +distinct
    +engine_from_config
    +except_
    +except_all
    +exists
    +extract
    +false
    +func
    +funcfilter
    +insert
    +inspect
    +intersect
    +intersect_all
    +join
    +lateral
    +literal
    +literal_column
    +modifier
    +not_
    +null
    +nullsfirst
    +nullslast
    +or_
    +outerjoin
    +outparam
    +over
    +select
    +subquery
    +table
    +tablesample
    +text
    +true
    +tuple_
    +type_coerce
    +union
    +union_all
    +update
    +within_group
    +['AliasOption', 'AttributeExtension', 'Bundle', 'ColumnProperty', 'ComparableProperty', 'CompositeProperty', 'EXT_CONTINUE', 'EXT_SKIP', 'EXT_STOP', 'Load', 'Mapper', 'MapperExtension', 'PropComparator', 'Query', 'RelationshipProperty', 'Session', 'SessionExtension', 'SynonymProperty', 'aliased', 'backref', 'class_mapper', 'clear_mappers', 'close_all_sessions', 'column_property', 'comparable_property', 'compile_mappers', 'composite', 'configure_mappers', 'contains_alias', 'contains_eager', 'create_session', 'defaultload', 'defer', 'deferred', 'dynamic_loader', 'eagerload', 'eagerload_all', 'foreign', 'immediateload', 'join', 'joinedload', 'joinedload_all', 'lazyload', 'lazyload_all', 'load_only', 'make_transient', 'make_transient_to_detached', 'mapper', 'noload', 'object_mapper', 'object_session', 'outerjoin', 'polymorphic_union', 'public_factory', 'query_expression', 'raiseload', 'reconstructor', 'relation', 'relationship', 'remote', 'scoped_session', 'selectin_polymorphic', 'selectinload', 'selectinload_all', 'sessionmaker', 'subqueryload', 'subqueryload_all', 'synonym', 'undefer', 'undefer_group', 'validates', 'was_deleted', 'with_expression', 'with_parent', 'with_polymorphic']
    +AliasOption
    +AttributeExtension
    +Bundle
    +ColumnProperty
    +ComparableProperty
    +CompositeProperty
    +EXT_CONTINUE
    +EXT_SKIP
    +EXT_STOP
    +Load
    +Mapper
    +MapperExtension
    +PropComparator
    +Query
    +RelationshipProperty
    +Session
    +SessionExtension
    +SynonymProperty
    +aliased
    +backref
    +class_mapper
    +clear_mappers
    +close_all_sessions
    +column_property
    +comparable_property
    +compile_mappers
    +composite
    +configure_mappers
    +contains_alias
    +contains_eager
    +create_session
    +defaultload
    +defer
    +deferred
    +dynamic_loader
    +eagerload
    +eagerload_all
    +foreign
    +immediateload
    +join
    +joinedload
    +joinedload_all
    +lazyload
    +lazyload_all
    +load_only
    +make_transient
    +make_transient_to_detached
    +mapper
    +noload
    +object_mapper
    +object_session
    +outerjoin
    +polymorphic_union
    +public_factory
    +query_expression
    +raiseload
    +reconstructor
    +relation
    +relationship
    +remote
    +scoped_session
    +selectin_polymorphic
    +selectinload
    +selectinload_all
    +sessionmaker
    +subqueryload
    +subqueryload_all
    +synonym
    +undefer
    +undefer_group
    +validates
    +was_deleted
    +with_expression
    +with_parent
    +with_polymorphic
    +
    +
    +
    + +
    +
    + +
    +
    +
    +
    In [ ]:
    +
    +
    +
     
    +
    + +
    +
    +
    + +
    +
    +
    + + + + + + diff --git a/notebook/static/modules/sqlalchemy/index.html b/notebook/static/modules/sqlalchemy/index.html new file mode 100644 index 0000000..c5d41f8 --- /dev/null +++ b/notebook/static/modules/sqlalchemy/index.html @@ -0,0 +1,14 @@ + + + + + Error response + + +

    Directory listing for ./static/modules/sqlalchemy

    +
    + +
    + + diff --git a/notebook/static/modules/urlencode.html b/notebook/static/modules/urlencode.html new file mode 100644 index 0000000..3609ee5 --- /dev/null +++ b/notebook/static/modules/urlencode.html @@ -0,0 +1,13332 @@ + + + + +urlencode + + + + + + + + + + + + + + + + + + + + + + +
    +
    + +
    +
    +
    In [1]:
    +
    +
    +
    # https://www.urlencoder.io/python/
    +import urllib
    +urllib.parse.quote('w xnacy')
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[1]:
    + + + + +
    +
    'w%20xnacy'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [2]:
    +
    +
    +
    urllib.parse.quote_plus('w xnacy')
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[2]:
    + + + + +
    +
    'w+xnacy'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [3]:
    +
    +
    +
    urllib.parse.quote('/')
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[3]:
    + + + + +
    +
    '/'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [4]:
    +
    +
    +
    urllib.parse.quote('/', safe='')
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[4]:
    + + + + +
    +
    '%2F'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [5]:
    +
    +
    +
    params = dict(text = 'name is wxnacy', url='https://wxnacy.com')
    +urllib.parse.urlencode(params)
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[5]:
    + + + + +
    +
    'text=name+is+wxnacy&url=https%3A%2F%2Fwxnacy.com'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [6]:
    +
    +
    +
    urllib.parse.urlencode(params, quote_via=urllib.parse.quote)
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[6]:
    + + + + +
    +
    'text=name%20is%20wxnacy&url=https%3A%2F%2Fwxnacy.com'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [7]:
    +
    +
    +
    params = {'name': 'Rajeev Singh', 'phone': ['+919999999999', '+628888888888']}
    +urllib.parse.urlencode(params, doseq=True)
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[7]:
    + + + + +
    +
    'name=Rajeev+Singh&phone=%2B919999999999&phone=%2B628888888888'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [ ]:
    +
    +
    +
     
    +
    + +
    +
    +
    + +
    +
    +
    + + + + + + diff --git a/notebook/static/signature.html b/notebook/static/signature.html new file mode 100644 index 0000000..133dced --- /dev/null +++ b/notebook/static/signature.html @@ -0,0 +1,13272 @@ + + + + +signature + + + + + + + + + + + + + + + + + + + + + + +
    +
    + +
    +
    +
    In [90]:
    +
    +
    +
    # Python Version: 3.7
    +# 计算签名
    +# https://help.aliyun.com/document_detail/28761.html?spm=a2c4g.11186623.6.791.66725328JBIR5G
    +
    +
    +#https://sts.aliyuncs.com/?&AccessKeyId=testid&SignatureMethod=HMAC-SHA1&Version=2015-04-01&Action=AssumeRole&SignatureNonce=571f8fb8-506e-11e5-8e12-b8e8563dc8d2
    +params = {
    +    "Action": "AssumeRole",
    +    "Format": "JSON",
    +    "Version": "2015-04-01",
    +    "SignatureMethod": "HMAC-SHA1",
    +    "SignatureNonce": "571f8fb8-506e-11e5-8e12-b8e8563dc8d2",
    +    "SignatureVersion": "1.0",
    +    "AccessKeyId": "testid",
    +    "Timestamp": "2015-09-01T05:57:34Z",
    +    "RoleArn": "acs:ram::1234567890123:role/firstrole",
    +    "RoleSessionName": "client"
    +}
    +
    +# 按照首字母排序
    +params_arr = list(params.items())
    +params_arr.sort(key=lambda x: x[0])
    +params_arr
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[90]:
    + + + + +
    +
    [('AccessKeyId', 'testid'),
    + ('Action', 'AssumeRole'),
    + ('Format', 'JSON'),
    + ('RoleArn', 'acs:ram::1234567890123:role/firstrole'),
    + ('RoleSessionName', 'client'),
    + ('SignatureMethod', 'HMAC-SHA1'),
    + ('SignatureNonce', '571f8fb8-506e-11e5-8e12-b8e8563dc8d2'),
    + ('SignatureVersion', '1.0'),
    + ('Timestamp', '2015-09-01T05:57:34Z'),
    + ('Version', '2015-04-01')]
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [91]:
    +
    +
    +
    from collections import OrderedDict
    +import urllib.parse
    +
    +# 拼接 sign_str
    +sign_str = "GET&%2F&"
    +sign_dict = OrderedDict(params_arr)
    +params_str = urllib.parse.urlencode(sign_dict)
    +params_str = urllib.parse.quote(params_str)
    +
    +sign_str += params_str
    +sign_str
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[91]:
    + + + + +
    +
    'GET&%2F&AccessKeyId%3Dtestid%26Action%3DAssumeRole%26Format%3DJSON%26RoleArn%3Dacs%253Aram%253A%253A1234567890123%253Arole%252Ffirstrole%26RoleSessionName%3Dclient%26SignatureMethod%3DHMAC-SHA1%26SignatureNonce%3D571f8fb8-506e-11e5-8e12-b8e8563dc8d2%26SignatureVersion%3D1.0%26Timestamp%3D2015-09-01T05%253A57%253A34Z%26Version%3D2015-04-01'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [87]:
    +
    +
    +
    # 验证签名用的 str 是否正确
    +eg = "GET&%2F&AccessKeyId%3Dtestid%26Action%3DAssumeRole%26Format%3DJSON%26RoleArn%3Dacs%253Aram%253A%253A1234567890123%253Arole%252Ffirstrole%26RoleSessionName%3Dclient%26SignatureMethod%3DHMAC-SHA1%26SignatureNonce%3D571f8fb8-506e-11e5-8e12-b8e8563dc8d2%26SignatureVersion%3D1.0%26Timestamp%3D2015-09-01T05%253A57%253A34Z%26Version%3D2015-04-01"
    +assert sign_str == eg
    +
    + +
    +
    +
    + +
    +
    +
    +
    In [82]:
    +
    +
    +
    import hashlib
    +import hmac
    +import base64
    +h = hmac.new('testsecret&'.encode('utf-8'), sign_str.encode('utf-8'), hashlib.sha1)
    +sign = h.digest()
    +sign = base64.b64encode(sign).decode()
    +sign
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[82]:
    + + + + +
    +
    'gNI7b0AyKZHxDgjBGPDgJ1Ce3L4='
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [89]:
    +
    +
    +
    # 验证签名值
    +assert sign == 'gNI7b0AyKZHxDgjBGPDgJ1Ce3L4='
    +
    + +
    +
    +
    + +
    +
    +
    +
    In [ ]:
    +
    +
    +
     
    +
    + +
    +
    +
    + +
    +
    +
    + + + + + + diff --git a/notebook/static/test.html b/notebook/static/test.html new file mode 100644 index 0000000..d4e6b62 --- /dev/null +++ b/notebook/static/test.html @@ -0,0 +1,13228 @@ + + + + +test + + + + + + + + + + + + + + + + + + + + + + +
    +
    + +
    +
    +
    In [1]:
    +
    +
    +
    !echo $PYTHONPATH
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    + + +
    +
    /Users/wxnacy/PycharmProjects/study/notebook
    +
    +
    +
    + +
    +
    + +
    +
    +
    +
    In [3]:
    +
    +
    +
    import config
    +config.DATABASE_URL
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[3]:
    + + + + +
    +
    'mysql://root:wxnacy@127.0.0.1:3306/study?charset=utf8mb4'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [6]:
    +
    +
    +
    import os;os.path.dirname('./test.html'.replace('./', './static/'))
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[6]:
    + + + + +
    +
    './static'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [4]:
    +
    +
    +
    './test.html'.replace('./', './static')
    +
    + +
    +
    +
    + +
    +
    + + +
    + +
    Out[4]:
    + + + + +
    +
    './statictest.html'
    +
    + +
    + +
    +
    + +
    +
    +
    +
    In [ ]:
    +
    +
    +
     
    +
    + +
    +
    +
    + +
    +
    +
    + + + + + + diff --git a/notebook/test.ipynb b/notebook/test.ipynb new file mode 100644 index 0000000..c106985 --- /dev/null +++ b/notebook/test.ipynb @@ -0,0 +1,138 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "/Users/wxnacy/PycharmProjects/study/notebook\r\n" + ] + } + ], + "source": [ + "!echo $PYTHONPATH" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'mysql://root:wxnacy@127.0.0.1:3306/study?charset=utf8mb4'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import config\n", + "config.DATABASE_URL" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "./static\n" + ] + } + ], + "source": [ + "import os;print(os.path.dirname('./test.html'.replace('./', './static/')))" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'./statictest.html'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'./test.html'.replace('./', './static')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%E9%9D%A2%E8%AF%95%E9%A2%9864.%E6%B1%821%2B2%2B%E2%80%A6%2Bn" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'%E9%9D%A2%E8%AF%95%E9%A2%9864.%E6%B1%821%2B2%2B%E2%80%A6%2Bn.html'" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "url = '面试题64.求1+2+…+n.html'\n", + "import urllib\n", + "urllib.parse.quote(url)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}