沒有人的文庫


  • 首頁

  • 歸檔

  • 標籤

Use GNU Utilities on macOS

發表於 2017-06-21 |

Some commands are identical on macOS and GNU/Linux, but they behave differently, e.g., readlink and sed. We can install GNU version commands via Homebrew, all commands are installed with the prefix g by default, e.g., greadlink and gsed.

brew install coreutils provides:

  • greadlink
  • grealpath
  • etc.

brew install gnu-sed provides: gsed

Use flock to Ensure Only One Instance of a Shell Script Is Running

發表於 2017-02-16 |
1
2
3
4
5
6
7
8
#!/usr/bin/env bash
script=$(realpath "$0")
lock_file="${script%.*}.lock"
(
flock -n 9 || exit 1
sleep 10 && echo 'Done!'
) 9> "$lock_file" && rm "$lock_file"

References:

  • Unix manual flock(1)

Use fcntl.flock to Ensure Only One Instance of a Python Script Is Running

發表於 2017-02-09 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python3
import fcntl
from os import remove
from os.path import splitext
from time import sleep
def main():
lock_file = '{}.lock'.format(splitext(__file__)[0])
with open(lock_file, 'w') as lock_fp:
try:
fcntl.flock(lock_fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
print('Script has been running.')
return
else:
print('Script starts to run.')
sleep(10)
print('Done!')
remove(lock_file)
if __name__ == '__main__':
main()

Note that this code doesn’t works on Windows.

References:

  • https://docs.python.org/3/library/fcntl.html#fcntl.flock

Fork 的工作流程

發表於 2016-06-11 |

習慣上我會保持 fork 的 master 和原始 repository 的 master 同步,並且不會直接 commit 自己的變更到該分支。

設定

首先為 fork 設定原始 repository 的 remote:

1
git remote add upstream <remote URL to original repository>

完成後輸入 git remote -v 應該會看到 origin 和 upstream。

然後再從 master 建立新的分支:

1
git checkout -b <my changes>

此後所有變更都 commit 到該分支上。

同步

執行:

1
2
3
git checkout master
git fetch upstream
git merge upstream/master

或是:

1
2
git checkout master
git pull upstream master

以同步 fork 的 master 分支。然後再執行:

1
2
git checkout <my changes>
git merge master

Python 不安裝額外套件轉換時區

發表於 2016-05-01 |

目標

將符合 ISO 8601 格式的 1987-09-12T09:12:00+08:00 字串轉換成 UTC 時區的 datetime.datetime 物件。

步驟

Python 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from datetime import datetime, timezone
def main():
dt_format = '%Y-%m-%dT%H:%M:%S%z'
dt_str = '1987-09-12T09:12:00+08:00'
dt = datetime.strptime(dt_str.replace('+08:00', '+0800'), dt_format)
utc_dt = dt.astimezone(timezone.utc)
print(dt) # 1987-09-12 09:12:00+08:00
print(utc_dt) # 1987-09-12 01:12:00+00:00
if __name__ == '__main__':
main()

Python 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from datetime import datetime, timedelta, tzinfo
class UTC(tzinfo):
def utcoffset(self, dt):
return timedelta(0)
def tzname(self, dt):
return 'UTC'
def dst(self, dt):
return timedelta(0)
class GMT8(tzinfo):
def dst(self, dt):
return timedelta(0)
def tzname(self, dt):
return 'GMT +8'
def utcoffset(self, dt):
return timedelta(hours=8) + self.dst(dt)
def main():
dt_format = '%Y-%m-%dT%H:%M:%S'
dt_str = '1987-09-12T09:12:00+08:00'
naive_dt = datetime.strptime(dt_str.replace('+08:00', ''), dt_format)
dt = naive_dt.replace(tzinfo=GMT8())
utc_dt = dt.astimezone(UTC())
print(dt) # 1987-09-12 09:12:00+08:00
print(utc_dt) # 1987-09-12 01:12:00+00:00
if __name__ == '__main__':
main()

建立可用於 AWS Lambda 的 Python 部署套件

發表於 2015-12-21 |

前言

這篇主要是紀錄實作 AWS Lambda 開發者文件 Creating a Deployment Package (Python) 的步驟,同時還參考了 Running Python with compiled code on AWS Lambda 這篇文章。

實作

開個跑 Amazon Linux AMI 的 EC2 instance

AWS lambda 的執行環境基本上就是 Amazon Linux AMI,因為使用同樣環境下編譯的套件比較容易成功,所以我們首先要開個跑 Amazon Linux AMI 的 EC2 instance。

安裝編譯 lxml 所需要的套件

ssh 進剛剛開好的 EC2 instance 並安裝套件:

1
$ sudo yum install gcc libxml2-devel libxslt-devel

在虛擬環境安裝 lxml

Amazon Linux AMI 預設的 python 環境已經裝了很多不必要的套件,因此我們需要用 virtualenv 建立乾淨的虛擬環境:

1
$ python -m virtualenv path/to/my/virtual-env

建立好之後啟動虛擬環境:

1
$ source path/to/my/virtual-env/bin/activate

安裝 lxml:

1
$ python -m pip install lxml

打包 lambda 程式

假設用到 lxml 的 lambda 程式放在 path/to/my/project 下,先切換到該目錄再打包成 zip 檔:

1
2
$ cd path/to/my/project
$ zip -9r bundle.zip *

然後再切換到該虛擬環境放置套件的目錄,並打包進剛剛的 zip 檔:

1
2
3
4
$ cd $VIRTUAL_ENV/lib/python2.7/site-packages
$ zip -9r path/to/my/project/bundle.zip *
$ cd $VIRTUAL_ENV/lib64/python2.7/site-packages
$ zip -9r path/to/my/project/bundle.zip *

發佈

最後只要上傳 path/to/my/project/bundle.zip 到 AWS Lambda 去就完成了。

令 Atom 顯示合字

發表於 2015-12-13 |

有些字型如 Hasklig、Fira Code 和 Monoid 有專為程式語言設計的合字(ligature),可以讓程式碼更好閱讀。

要讓 Atom 顯示這些合字,除了設定使用這種字型外,還得在 ~/.atom/styles.less(*nix)或是 %USERPROFILE%\.atom\styles.less(Windows)內加入樣式:

1
2
3
.editor {
text-rendering: optimizeLegibility;
}

註:可以從選單列 Atom > Open Your Stylesheet(*nix)或是 File > Open Your Stylesheet(Windows)快速開啟這個檔案。

Pei-Lun H.

Pei-Lun H.

7 文章
9 標籤
RSS
© 2017 Pei-Lun H.
由 Hexo 強力驅動
主題 - NexT.Muse