Posts

Showing posts with the label 小抄

用 Mac 終端機操作 mongodb

寫筆記時的執行環境 MacOS Big Sur Version 11.4 MongoDB shell version v4.2.12 啟動資料庫 <path to binary>/mongod --dbpath <path to data directory> 參考資料: Install MongoDB 用 Mac 終端機連接資料庫 <path to binary>/mongo 操作指令 列出所有資料庫: show dbs 使用某個資料庫: use <db name> (沒設定的話預設是使用 test) 列出目前資料庫中的所有 collection: show collections 查詢目前資料庫中某個 collection 的資料: db.<collection name>.find({}) or db.<collection name>.find({}).pretty() (撈出來的資料會排版得好看一點) 在目前資料庫中某個 collection 裡新增一筆資料: db.<collection name>.insert({}) 在目前資料庫中某個 collection 裡新刪除符合條件的資料: db.<collection name>.remove({}) 最後更新日期:2021 年 8 月 9 日

flask 小抄(pip + virtualenv 版)(Flask 2.0.1)

初始化專案 建立並移動到專案資料夾 建立虛擬環境: pyenv exec python -m venv .venv 啟動虛擬環境: . .venv/bin/activate ,或 source .venv/bin/activate 安裝 Flask 套件: pip install Flask 在根目錄建立 app.py 設定環境變數: export FLASK_APP=app.py export FLASK_ENV=development (存擋後會自動重啟 app) .gitignore: Python.gitignore Application Factories  起手式: # app.py from flask import Flask, render_template, request def create_app():     app = Flask(__name__)     # do something     return app 執行專案 flask run 在瀏覽器網址列輸入 http://127.0.0.1:5000/ Jinja2 Builtin Filter 套件管理 requirements.txt 格式: Requirement Specifiers 、 Version specifiers 範例: EXAMPLE REQUIREMENTS FILE 列出已安裝的套件: pip freeze 連接資料庫 mongodb 安裝套件(先確定有啟動 venv): pip install pymongo or support for mongodb+srv:// URIs requires dnspython: pip install pymongo[srv] PyMongo 3.12....

[Udemy] Python 3: Deep Dive 系列課程備忘錄

Part 1 - Functional Section 6 影片編號:85 dir([object]):查詢物件屬性 inspect module 影片編號:90、91 functools.reduce(function, iterable[, initializer]) min(iterable), max(iterable), sum(iterable), any(iterable), all(iterable) 把遞迴改寫成 reduce:程式碼在影片 #91 時間 18:29 處 影片編號:92 functools.partial(func, /, *args, **keywords) 回傳某些參數已經被固定了的 func 可能的坑:呼叫「某些參數已經被固定了的 func」時,仍可以覆寫被固定住的參數值 def power(base, exponent):     return base ** exponent     square = partial(power, exponent=2)     print(square(5, exponent=3)) 影片編號:93 partial 可能的應用場合: sorted(iterable, *, key=None, reverse=False)、list.sort(*, key=None, reverse=False) 的 key 只能放一個參數的函式 影片編號:94 operator module :getitem(a, b)、 itemgetter(item)、attrgetter(attr) Section 7 影片編號:97 宣告 global 變數 when python encounters a function definition at compilt-time when function is...

MySQL 初始化小抄(使用環境:node.js)

安裝 MySQL 和 Workbrench 在 Workbrench 建立 DATABASE CREATE DATABASE 資料庫名稱; 安裝套件 npm install mysql2 sequelize sequelize-cli 執行初始化腳本 npx sequelize init 初始化腳本會建立四個東西 config/config.json 修改資料庫名稱及登入密碼。 (註:operatorsAliases 在 Sequelize v5 後已棄用) models/index.js migrations seeders 設定 model 和 seed: 官方文件

專案初始化小抄(工具:node.js + express + handlebars)

用終端機移動到專案資料夾後,用下列指令產生 package.json 檔 npm init -y 修改 package.json 檔,設定入口檔案及腳本,例如: "main": "app.js", "scripts": {     "start": "node app.js",     "dev": "nodemon app.js",     "test": "echo \"Error: no test specified\" && exit 1"   }, 安裝套件,例如 npm install express express-handlebars body-parser method-override 實用套件: body-parser:解析 POST request 的 request body method-override:瀏覽器只有 GET 和 POST 兩種請求方法。為了達成 RESTful API 的設計風格或其他理由,用 method-override 將請求方法改成別的名字,例如 PUT、DELETE 等等 express-session:為 http 請求加入狀態 passport:使用者驗證。不同的驗證方式,需使用對應的驗證策略 passport-local:在本地端進行驗證的驗證策略 bcryptjs:把機密資料(例如使用者的密碼)加鹽並雜湊 faker:產生假資料 multer:上傳檔案 helmet:設定與資訊安全有關的 HTTP 標頭 sanitize-html:消毒 HTML 建立入口檔案,打開該檔案做基本設定: const express = require('express') const exphbs = require('express-handlebars') const bodyParser = require('b...

CSS 起手式

Reset CSS /* http://meyerweb.com/eric/tools/css/reset/     v2.0 | 20110126     License: none (public domain) */ html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {   margin: 0;   padding: 0;   border: 0;   font-size: 100%;   font: inherit;   vertical-align: baseline; } /* HTML5 display-role reset for older browsers */ article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section {   display: block; } body {   line-height: 1; } ol, ul {   ...

HTML 起手式

 <!DOCTYPE html> <html> <head> <meta charset=UTF-8>                          <meta name="viewport" content="width=device-width, initial-scale=1"> <title>                   </title>                     <!--在 head 末端引入 js 檔案-->                     <script type="text/javascript" src="檔案網址"></script>  </head> <body>                     <!--在 body 末端引入 js 檔案-->                     <script type="text/javascript" src="檔案網址"></script>   </body> </html>