2019年3月18日星期一

My Python Cheat Sheet

Strings

x = 'a'
x.isalpha()
>>> True
x.isdigit()
>>> False

eval( ' 1*2+3 ' )
>>> 5

ord()
>>>ord('a') 97 >>> ord('b') 98 >>> ord('c') 99


Turn string to lower/upper case
s.lower()
s.upper()
---------------------------------------------------
Numbers

import sys
sys.maxsize # get a large number
-sys.maxsize - 1

cmp(a, b)
-1 if a<b
0 if a=b
1 if a>b

sort by x[0] then by x[1]
temp = sorted(temp, key = lambda x: (x[0], x[1]))

---------------------------------------------------
Lists

sorted(list)
list.sort()

reversed(list)
list.reverse()
list[::-1]

find()
>>> str1 = "this is string example....wow!!!";
>>> str1.find('tri')
9

Counter()
tasks = [A, A, A, B, B, B, B]
d = collections.Counter(tasks)
print(d)
>>> Counter({'A': 3, 'B': 4})

counts = d.values()
print(counts)
>>> dict_values([3, 4])


---------------------------------------------------
Deque
import collections

de = collections.deque([1,2,3])
de.append(4)
de.appendleft(4)
de.pop()
de.popleft()

---------------------------------------------------

Random Library
random.randint(min, max)

pick a random element from a list:
random.choice(list)

pick a random element from a set:
random.sample(set, 1)[0]
random.choice(list(set))

---------------------------------------------------
Binary to decimal
A = int(a, 2)

Decimal to binary
bin(A)

2018年9月11日星期二

Deploying Node+Mongodb+JWT to Heroku

When setting JWT secret in develope or test environment:

config/config.js
var env = process.env.NODE_ENV || 'development'; // for heroku deploy


if (env === 'development' || env === 'test') {
var config = require('./config.json');
var envConfig = config[env];
Object.keys(envConfig).forEach((key) => {
// loops through the env Object to copy the setting values from config.json to process.env
process.env[key] = envConfig[key]
});
}

config/config.json
{
"test": {
"PORT": 3000,
"MONGODB_URI": "mongodb://localhost:27017/ResuMakerTest",
"JWT_SECRET": "qwertyuihjkljklopIdonthavetorememberthis"
},
"development": {
"PORT": 3000,
"MONGODB_URI": "mongodb://localhost:27017/ResuMaker",
"JWT_SECRET": "qwertyuiop123456thisisrandom"
}
}

However, when deploying to Heroku, the JWT secret have to be set by Heroku CLI:


heroku config:set JWT_SECRET=mysecretvalue

We can check if the JWT_SECRET has been properly set up by running heroku config and view all variables.



2018年2月5日星期一

How to copy local file to SSH server using terminal


Go to your local directory


scp project_0 hsiehke@flip.engr.oregonstate.edu:~/CS575

CS 575
g++ -o proj proj.cpp -lm -fopenmp

icpc -o proj5 proj5.cpp -lm -openmp -align -qopt-report=3 -qopt-report-phase=vec -no-vec

-----
CS 519-10

flip $ /nfs/farm/classes/eecs/winter2018/cs519-010/submit hw10 rna.py

2017年10月17日星期二

Setup DB for DeadDrop Project


You can make sure if your mysql is correctly installed by running Chris's Node seminar's mysql project and try putting your name on there.

In the deaddrop project directory, open config/db.json
change the "user" and "password" to match your local settings
(you can view the db.json in the seminar's mysql project as reference)

Login to mysql in the terminal

/usr/local/mysql/bin/mysql --user root -p
in the deaddrop repo, open MySQL DB Setup Script.sql

paste the first line to create database
CREATE DATABASE `deaddrop` /*!40100 DEFAULT CHARACTER SET utf8 */;

In terminal, type this to change the database
use deaddrop
then paste the rest to create table

CREATE TABLE `messages` (
`uuid` int(11) NOT NULL AUTO_INCREMENT,
`message` varchar(300) NOT NULL,
`timestamp` datetime NOT NULL,
`latitude` decimal(13,10) NOT NULL,
`longitude` decimal(13,10) NOT NULL,
PRIMARY KEY (`uuid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;


Grant user semdemo to use this db
grant all privileges on deaddrop . * to 'semdemo'@'localhost';

run this in the deaddrop repo
sudo npm start dev
and the website will be at localhost:443





2017年10月14日星期六

Unit Test筆記:如何測試TableView

















Using tableViewController


Unit Test筆記:setUp() & tearDown()

為了不讓多個test case撞在一起,
在每個test case的前與後就會加入 setUp() 與 tearDown()


所以在每個 test 之前,如果 DropManager.drops.count 不等於3的話,就會自動重置
並且在 test 結束後清空 DropManager.drops 陣列



舉例來說,

由於DropManager是Singleton

所以如果這兩個test case同時存在的情況


如果沒有 setUp tearDown 的情況下,一次測試,
testAddDrop2 會出現錯誤,DropManager.drops.count 應該等於 5

但有 setUp tearDown 後,就能在兩個test case之間重置 DropManager,使兩個 test case 都能 pass

Unit Test 筆記:最基礎的Unit Test

如果沒有在一開始就生成Unit Test的話,可以從Project頁面的下方加入



















然後選擇加入Unit Test



















在test file的第一行,要加入 @testable import APP的名字
這一段的作用是聯繫app本體,才能從test file中讀取app的數據進行測試




然後就可以開始寫測試了
這裡的Drop和Drop Manager都是Classes裡面宣告的Object
所以暫時沒有指定到View Controller的狀況

XCTAssertEqual是檢查左右數值是否相等



值得注意的是,test中會把View Controller的程式碼也一起運行
DropManager.drops.count 如果沒有在其中一個 VC 的 viewDidLoad 進行 init() 的話
這裏的 DropManager.drops.count 就會等於 1


因為他原本長這樣