Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ゼロから始める Python パッケージ配布

chanyou0311
September 27, 2021

ゼロから始める Python パッケージ配布

chanyou0311

September 27, 2021

Transcript

28 slides
  1. ライセンスを決める ライセンスのないソフトウェアを他人が利用することはできない Choose an open source license | Choose a

    License 今回は練習の意味合いが強かったので NYSL にしました 煮るなり焼くなり好きにしろライセンス 8
  2. こんな感じで作成される $ tree hello-world hello-world ├── README.rst ├── pyproject.toml ├──

    src │ └── hello_world │ └── __init__.py └── tests ├── __init__.py └── test_hello_world.py 3 directories, 5 files 10
  3. import argparse import time def countdown_sleep(second: int): # check second

    is natural number (contains 0) if not isinstance(second, int) or second < 0: raise ValueError("'second' should be a natural number (contains 0). second: %s" % second) # countdown for i in range(second, 0, -1): print(f"\b \b\r{i}", end="", flush=True) time.sleep(1) def cli(): parser = argparse.ArgumentParser(description='Sleep and print countdown.') parser.add_argument('second', type=int, help='sleep seconds') args = parser.parse_args() countdown_sleep(args.second) 12
  4. パッケージをテストする 雛形に pytest が入っている $ poetry run pytest ================================== test

    session starts =================================== platform darwin -- Python 3.9.4, pytest-5.4.3, py-1.10.0, pluggy-0.13.1 rootdir: ... collected 1 item tests/test_hello_world.py . [100%] =================================== 1 passed in 0.01s ==================================== 13
  5. @patch("time.sleep", return_value=None) def test_print_countdown_0(mock_sleep): n = 0 expect_output = ""

    with captured_stdout() as stdout: countdown_sleep(n) assert stdout.getvalue() == expect_output @patch("time.sleep", return_value=None) def test_call_sleep_10_times(mock_sleep): n = 10 countdown_sleep(n) assert mock_sleep.call_count == 10 @patch("time.sleep", return_value=None) def test_raise_value_error_negative_1(mock_sleep): n = -1 with pytest.raises(ValueError): countdown_sleep(n) 14
  6. いい感じ poetry run pytest ================================== test session starts =================================== platform

    darwin -- Python 3.9.4, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: ... collected 7 items tests/test_countdown_sleep.py ....... [100%] =================================== 7 passed in 0.06s ==================================== 15
  7. pyproject.toml にパッケージの名前や説明、バージョンなどを記入 する [tool.poetry] name = "countdown-sleep" version = "0.1.1"

    readme = "README.md" description = "Sleep and print countdown." license = "NYSL" include = ["CHANGELOG.md"] homepage = "https://github.com/chanyou0311/countdown-sleep" repository = "https://github.com/chanyou0311/countdown-sleep" authors = ["chanyou0311 <[email protected]>"] 17
  8. パッケージをビルドする $ poetry build Building hello-world (0.1.0) - Building sdist

    - Built hello-world-0.1.0.tar.gz - Building wheel - Built hello_world-0.1.0-py3-none-any.whl 18
  9. パッケージを公開する まずは TestPyPI に公開する いきなり本番の PyPI サーバーに公開せずに、テストサーバー TestPyPI · The

    Python Package Index で公開してみる 事前に TestPyPI のアカウントを作成しておく TestPyPI の設定を行う $ poetry config repositories.testpypi https://test.pypi.org/legacy/ $ poetry config http-basic.testpypi <USERNAME> <PASSWORD> 19
  10. 正しくインストールできるか確認する $ pip install --extra-index-url https://test.pypi.org/simple/ countdown-sleep Looking in indexes:

    https://pypi.org/simple, https://test.pypi.org/simple/ Collecting countdown-sleep Using cached countdown_sleep-0.1.1-py3-none-any.whl (3.2 kB) Installing collected packages: countdown-sleep Successfully installed countdown-sleep-0.1.1 22
  11. 使えた $ countdown_sleep --help usage: countdown_sleep [-h] second Sleep and

    print countdown. positional arguments: second sleep seconds optional arguments: -h, --help show this help message and exit 23
  12. わいわい $ pip install countdown-sleep Collecting countdown-sleep Using cached countdown_sleep-0.1.1-py3-none-any.whl

    (3.2 kB) Installing collected packages: countdown-sleep Successfully installed countdown-sleep-0.1.1 26
  13. LGTM $ countdown_sleep --help usage: countdown_sleep [-h] second Sleep and

    print countdown. positional arguments: second sleep seconds optional arguments: -h, --help show this help message and exit 27

More Decks by chanyou0311

Other Decks in Technology