[Django] 常用基本指令

  • 新建專案
django-admin startproject mysite
  • 新建app
python manage.py startapp myapp
  • 啟動sever
python manage.py runserver localhost:443
  • 建立super user
python manage.py createsuperuser

[Django] Manage django database using additional scripts

  • If you just want to use django ORM, please refer to Django ORM Standalone Template.
  • If you want to use other python code to manipulate existing django database inside a project, using its pretty ORM, please do as following code:
# setup django environment using existing settings file
import os
import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yourproject.settings")
django.setup()

# start of main code
from myapp.models import *

testobjs = mymodel.objects.all()
print(testobjs)

[SQL] 匯入.csv檔案以及相關問題

語法:

LOAD DATA INFILE "C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Uploads\\plane-data.csv" INTO TABLE plane
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(tailnum,type,manufacturer,@issue_date,model,status,aircraft_type,engine_type,year)
SET issue_date=str_to_date(@issue_date, "%m/%d/%Y");

問題1:
出現secure-file-priv限制檔案位置以及LOCAL語法使用

解法: 到my.ini,將此設定修改成空字串

secure-file-priv=""

問題2:
出現空值與attribute型態不符(如””incorrect for integer)

解法: 到my.ini,將此設定修改成空字串

sql-mode=""

問題3:
csv檔案中,空值並沒有表示成,,,而是直接空著

解法: 使用excel,另存成.csv檔案,即可自動使用,,分隔。

[C++] Measure of time elapsed

#include <ctime>
using namespace std;

clock_t begin = clock();
target_func();
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
cout << "Elapsed time: " << elapsed_secs << " sec." << endl;

[Git] 取消追蹤檔案(但是保留檔案)

Step1:

將欲取消追蹤的檔案加入.gitignore

Step2:

在repo中刪除該檔案:

git rm -r --cached .
git commit -m "Untrack and delete filename in repo."

Step3:

push到遠端

[Python] 虛擬環境簡易用法

建立虛擬環境

python -m venv mytargetdir

啟用虛擬環境

source activate myenv

關閉虛擬環境

source deactivate

export環境:

pip freeze > requirements.txt

import環境:

pip install -r requirements.txt

*conda請使用以下指令:

#新建virtual env:
conda create -n yourenvname python=x.x

#使用環境檔案建立虛擬環境
conda env create -f environment.yml

#查看所有env:
conda info -e

#啟用虛擬環境
conda activate yourenvname

#關閉虛擬環境
conda deactivate

#刪除虛擬環境:
conda env remove -n yourenvname

#匯出虛擬環境:
conda env export > environment.yml

#使用環境檔案安裝套件
conda env update -n yourenvname --file environment.yml

[Git] 解決conflict (官方)

Step 1: From your project repository, bring in the changes and test.

git fetch origin
git checkout -b steven origin/steven
git merge master

Step 2: Merge the changes and update on GitHub.

git checkout master
git merge --no-ff steven
git push origin master

Conflict with binary files:

git checkout --theirs -- path/to/conflicted-file.txt
git checkout --ours -- path/to/conflicted-file.txt

[Git] 合併commit

由新到舊 D -> C -> B -> A
要把D和C合併成單一個C’

Step1.

rebase -i <B的SHA-1>
若要rebase初始commit(A):
rebase -i --root

Step2.

pick <C的SHA-1> C的commit訊息
squash <D的SHA-1> D的commit訊息

Step3.

編輯C’的commit訊息

完成

P.S.若要push到github上則要強迫push:

git push --force

[Git] 常用指令

  • 初始化使用者資訊
git config --global user.email "my_email"
git config --global user.name "my_name"
  • 初始化git目錄
git init
  • 查看git狀態
git status
  • 一次加入所有更改檔案
git add .
git add -A
  • commit
git commit -m “commit message”
  • 操作遠端repo
# 顯示資訊
git remote -v
git remote show origin
# 刪除目前origin
git remote rm origin
# 新增origin
git remote add origin git@github.com:SongRongLee/xxxxx.git
  • 第一次push/pull
git push -u origin master
or
git branch -u origin/master
#以後只需要:
git push
  • 返回特定commit
git reset --hard HEAD^
git reset --hard HEAD~2
  • branch相關
# 以遠端分支為藍本建立本地new-branch
git checkout -b new-branch origin/master
# Delete a remote branch
git push -d origin bad-branch

# Delete a local branch
git branch -d bad-branch

# Sync remote branch information after deletion
git fetch --prune

# List all remote and local branches
git branch -a
# 修改branch名稱
git branch -m my-new-name
# push本地的local-branch到origin/remote-branch
git push origin local-branch:remote-branch
# Shows changes from from_branch to to_branch
git diff from_branch to_branch
  • stash相關
# pull但是保留local change
git stash
git pull
git stash pop

# Stash untracked files
git stash -u

# List all stashes
git stash list

# Pop a certain stash
git stash pop stash@{n}

# Stash with a given message
git stash -m my_message
  • 刪除first commit
git update-ref -d HEAD
  • Unstage all changes
git reset
  • Discard changes on one file
git checkout -- filename
  • Force overwrite local repo
git reset --hard origin/master
  • Move to next commit
git reset HEAD@{1}
  • Rename
git mv
  • Show ignored files
git status --ignored
  • Remove untracked files and directories
git clean -dn  # Dry run
git clean -df  # Actual remove
  • Check SHA-1 of commits from history
git reflog