Easter Eggs in Python

役に立たなそうな隠しコマンド的なやつたち。

Hello World

>>> import __hello__
Hello world!

The Zen of Python

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Antigravity

>>> import antigravity     

Braces is Not a chance…

>>> from __future__ import braces
File "<stdin>", line 1
SyntaxError: not a chance

Ansible Vaultを使ってみる

セキュアな情報を含んだ情報をgitとかに素の状態で置いておけないので、Ansible Vaultはファイルを暗号化してくれるらしい。別にAnsible自体を使わなくても、ansible-vaultコマンドとして利用できるようなので試してみる。

まず、インストールしてみる。とはいえ、Pythonがある前提。

$ pip install ansible-vault

これでansible-vaultコマンドが利用できるようになる。ということで、適当なファイルを作ってencryptしてみる。

$ echo secret > password.txt
$ ansible-vault encrypt password.txt
New Vault password:
Confirm New Vault password:
Encryption successful
$ cat password.txt
$ANSIBLE_VAULT;1.1;AES256
...

という感じで、password.txtの中身が変わっているのが確認できる。今度は復号化をしてみる。

$ ansible-vault decrypt password.txt 
Vault password:
Decryption successful
$ cat password.txt
secret

パスワードはencrypt時に入力したものを入れる。password.txtが元に戻ったことを確認できた。

ansibleに関係なく手軽に利用できるので便利かもしれない。

PyPIにリリースする方法

Pythonで利用するモジュールたちはpipでインストールできると便利かと思いますが、pipでインストールできるようなモジュールを作るためにはPyPIにリリースする必要があります。簡単にリリースできるので、その方法をまとめると、まずは、

  • PyPI: https://pypi.python.org/
  • テスト用PyPI: https://testpypi.python.org/

にそれぞれでアカウントを登録します。テスト用はリリーステスト用として利用できます。アカウントは共通ではないので、それぞれに作る必要があります。そしたら、~/.pypircを作成します。

[distutils]
index-servers =
  pypi
  pypitest
[pypi]
repository=https://pypi.python.org/pypi
username=アカウント名
password=パスワード
[pypitest]
repository=https://testpypi.python.org/pypi
username=アカウント名
password=パスワード


次にリリースに必要なモジュールをインストールします。

$ pip install wheel
$ pip install twine


あとはリリースしたいモジュールを作成しておき(つまり、setup.pyとか)、まずはリリース物を作成します。

$ python setup.py sdist bdist_wheel


テスト用サイトへリリースします。

$ twine register dist/*.tar.gz -r pypitest
$ twine register dist/*.whl -r pypitest
$ twine upload dist/* -r pypitest


順に実行して特にエラーが出なければ、https://testpypi.python.org/で確認します。
テスト用サイトで問題がなければ、本番リリースします。

$ twine register dist/*.tar.gz -r pypitest
$ twine register dist/*.whl -r pypitest
$ twine upload dist/* -r pypitest


あとは、https://pypi.python.org/で確認します。
以上でリリース完了です。