2017/11/8
mac - 移除所有 ANSI escape code
markdown
當你用 rspec 跑測試並且生成結果檔案時,檔案的內容可能會包含 [ANSI escape code](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors) 所表示的顏色。
舉例來說,如果你用以下程式跑測試:
```
rspec --format documentation --out result.txt
```
你可以使用 cat 去讀他
```
cat result.txt
```
結果看起來像這樣:
但是打開 result.txt 時,你會看到這樣的東西:
```
...
Finished in 1.76 seconds (files took 6.55 seconds to load)
[32m1 example, 0 failures [0m
Randomized with seed 1
```
[32m [0m 代表的就是顏色,這被稱為 [ANSI escape code](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors)。
但是我希望讓他消失,在請教大大之後得到一個不錯的解法,有人用 node.js 做了一個簡單的指令[strip-ansi](https://github.com/chalk/strip-ansi-cli)。
安裝方法(如果你有node.js)
```
# 這裡是 bash
npm install --global strip-ansi-cli
```
使用方法
```
# 基本用法
strip-ansi 字串
# 透過 |
cat 輸入檔案路徑 | strip-ansi > 輸出檔案路徑
```
2017/11/6
Ruby - warning: toplevel constant B referenced by A::B
markdown
##問題
warning: toplevel constant B referenced by A::B
##成因
當 A 是一個 class 且 A::B 還沒有被定義時,ruby 找不到 A::B 時,若 B 有定義,就先使用 B, 而不是拋出 Module#const_missing。
```
class A
end
A::String
#warning: toplevel constant String referenced by A::String
```
但 rails 的 autoload 是透過修改 Module#const_missing 而完成的。也就是說,rails 還來不及 autoload 就已經被 toplevel constant 攔截了。
##解法一
在使用到 A::B 的檔案前面都加 require_dependency 'a/b'
##解法二
在使用到 B 的檔案後面加 require_dependency 'a/b' 確保 rails 不可能會有知道 B 的存在但不知道 A::B 的存在的可能發生。
參考文件
[http://stem.ps/rails/2015/01/25/ruby-gotcha-toplevel-constant-referenced-by.html](http://stem.ps/rails/2015/01/25/ruby-gotcha-toplevel-constant-referenced-by.html)
[https://stackoverflow.com/questions/18515100/warning-toplevel-constant-referenced](https://stackoverflow.com/questions/18515100/warning-toplevel-constant-referenced)
2017/10/30
Python - numpy 的使用方法
markdown
[NumPy Reference](https://docs.scipy.org/doc/numpy-1.13.0/reference/index.html)
數列生成
```
>>> np.zeros(5)
#array([ 0., 0., 0., 0., 0.])
>>> np.ones(5)
#array([ 1., 1., 1., 1., 1.])
>>> np.arange(5)
#array([0, 1, 2, 3, 4])
```
[更多的數列生成](https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.array-creation.html)
數列變形
```
>>> np.arange(12)
#array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> np.arange(12).reshape(2,6)
#array([[ 0, 1, 2, 3, 4, 5],
# [ 6, 7, 8, 9, 10, 11]])
>>> np.arange(12).reshape(3,4)
#array([[ 0, 1, 2, 3],
# [ 4, 5, 6, 7],
# [ 8, 9, 10, 11]])
>>> np.arange(12).reshape(4,3)
#array([[ 0, 1, 2],
# [ 3, 4, 5],
# [ 6, 7, 8],
# [ 9, 10, 11]])
>>> np.arange(12).reshape(6,2)
#array([[ 0, 1],
# [ 2, 3],
# [ 4, 5],
# [ 6, 7],
# [ 8, 9],
# [10, 11]])
>>> np.arange(12).reshape(12,1).flatten()
#array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
```
查看數列形狀
```
>>> np.arange(12).reshape(1,2,3,2)
#array([[[[ 0, 1],
# [ 2, 3],
# [ 4, 5]],
#
# [[ 6, 7],
# [ 8, 9],
# [10, 11]]]])
>>> np.arange(12).reshape(1,2,3,2).shape
#(1, 2, 3, 2)
```
[更多的數列操作](https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.array-manipulation.html)
2017/10/26
Rails - Apartment 的使用方法
markdown
#簡介
Apartment : [https://github.com/influitive/apartment](https://github.com/influitive/apartment)
這個套件的功能是讓你在 rails 可以透過切換 db schema,來存取不同的資料。
在 postgresql 中,預設的 schema 名稱為 public ,而所有的 table 都在 schema 'public' 下。
你可以簡單地把 schema 看成 namespace,在 apartment 中,這被稱為是 tenant。
#安裝步驟
請參考 : [https://github.com/influitive/apartment](https://github.com/influitive/apartment)
#使用方法
## 在 rails c 環境下
新增 tenant
```
Apartment::Tenant.create('abc')
```
這個指令會在 schema 'abc' 下複製 public 上的所有表格。所以 tenant 就是具有相同表格的 schema。
在以下的示範,看到 abc 的地方,代表的是要填入你的 schema name。
顯示目前所在的 tenant
```
Apartment::Tenant.current
# "public"
```
切換 tenant
```
Apartment::Tenant.switch!('abc')
Apartment::Tenant.current
# "abc"
Apartment::Tenant.switch!('public')
Apartment::Tenant.current
# "public"
Apartment::Tenant.switch('abc') do
Apartment::Tenant.current
# "abc"
end
Apartment::Tenant.current
# "public"
```
有暫時切換跟永久切換兩種
## 在 rails db 環境下
列出所有 schema
```
\dn
# List of schemas
# Name | Owner
#--------+----------
# public | postgres
# abc | etrex
# (2 rows)
```
顯示目前所在的 schema
```
SHOW search_path;
# search_path
#-----------------
# "$user", public
# (1 row)
```
切換 schema
```
SET search_path TO abc;
# SET
SET search_path TO 'abc';
# SET
```
字串要不要加引號都可以,當 search_path 的值不在 schema 的列表上時不會跳 error,可以想像成連接到一個空的 schema,而這樣並不代表新增了一個 schema。
在 select 的當下使用 schema
```
SELECT * FROM schema_name.table_name;
```
## 關於 excluded_models
在 apartment 提供的功能中有一個功能,這個功能是可以設定在切換 schema 的時候,讓某些表格不要被切換,也就是做成全域的表格。
舉例來說,假設我有兩個 table,是使用以下 code 所生成。
```
# 這裡是 bash
rails g model a
rails g model b a:references
rails db:migrate
```
然後我將 B 做成全域的表格
```
# 這裡是 /config/initializers/apartment.rb 約 18 行的位置
config.excluded_models = %w{ B }
```
然後建立一個 tenant 'abc' 並且切換過去,在這個環境下做測試
```
# 這裡是 rails c 環境下
Apartment::Tenant.create('abc')
Apartment::Tenant.switch!('abc')
```
對 A 做一些事情
```
A.count
# (3.8ms) SELECT COUNT(*) FROM "as"
# => 0
```
對 B 也做一些事情
```
B.count
# (0.5ms) SELECT COUNT(*) FROM "public"."bs"
# => 0
```
在這裡可以看到,當你對 B 進行操作時,其實是會強制加上 "public" 的,也就是說,對於 B ,不管在哪一個 tenant 下,都只會去存取 schema 'public' 下的表格,所以其他的 schema 的表格 bs 應該會都是空的。
嘗試在 tenant 'abc' 下新增資料
```
a = A.create()
# (0.2ms) BEGIN
# SQL (0.4ms) INSERT INTO "as" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", "2017-10-25 17:16:22.084365"], ["updated_at", "2017-10-25 17:16:22.084365"]]
b = B.create(a:a)
# (0.2ms) BEGIN
# SQL (1.5ms) INSERT INTO "public"."bs" ("a_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["a_id", 3], ["created_at", "2017-10-25 17:16:24.957753"], ["updated_at", "2017-10-25 17:16:24.957753"]]
# (0.2ms) ROLLBACK
#ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "bs" violates foreign #key constraint "fk_rails_ddf8c0c4b5"
#DETAIL: Key (a_id)=(3) is not present in table "as".
#: INSERT INTO "public"."bs" ("a_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"
# from (irb):31
```
這時候出大問題了,因為 A 建立在 abc 下,但是 B 建立在 public 下。而 public.bs 有一個外來鍵限制是 a_id 必須要在 public.as 中有出現。
使用 [pgAdmin](https://www.pgadmin.org/) 連進去看,對 db_name/Schemas/public/Tables/bs/Constraints/fk_rails_ddf8c04b5 按右鍵選 CREATE Script:
。
會看到生成的 script 內容為:
```
-- Constraint: fk_rails_ddf8c0c4b5
-- ALTER TABLE public.bs DROP CONSTRAINT fk_rails_ddf8c0c4b5;
ALTER TABLE public.bs
ADD CONSTRAINT fk_rails_ddf8c0c4b5 FOREIGN KEY (a_id)
REFERENCES public."as" (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION;
```
在這裡明確地指出跟 public.bs 有關係的表格是 public.as ,而不是其他 schema 下的 as,但是 Apartment 不處理,所以會出問題。
2017/10/19
Network debug 方法
markdown
## linux 環境下
查詢所有 process
```
ps
```
輸出
```
PID TTY TIME CMD
21968 ttys000 0:00.16 -bash
```
查詢 port 3000 被誰占用
```
lsof -i :3000
```
輸出
```
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ruby 20308 etrex 25u IPv4 0xd000000000000 0t0 TCP *:hbci (LISTEN)
```
刪除某個 process
```
kill 20308
```
強制刪除某個 process
```
kill -9 20308
```
## 參考文件
kill process:[https://blog.gtwang.org/linux/linux-kill-killall-xkill/](https://blog.gtwang.org/linux/linux-kill-killall-xkill/)
訂閱:
文章 (Atom)

