LoginSignup
1
0

はじめに

Pythonのライブラリを作成し、PyPIに公開することは、自分のコードを他の開発者と共有し、再利用性を高めるための素晴らしい方法です。
本記事では、私が作成した文字列操作ライブラリ「stringutils-happy」を例に、Pythonのライブラリをゼロから作成し、PyPIに公開する手順を解説します。

同じライブラリ名は権限がないとPyPIにアップできないので自分の決めたフォルダ名で作成するようにして下さい!

ライブラリの概要

stringutils-happyは、文字列操作に関するいくつかの便利な関数を提供するシンプルなライブラリです。このライブラリには以下の機能が含まれています:

  • 文字列を逆にする関数
  • 文字列の大文字小文字を反転する関数
  • 文字列の単語をカウントする関数

環境の準備

まず、Pythonの仮想環境を作成します。仮想環境は、プロジェクトごとに異なるパッケージを管理するために使用します。

ここのフォルダは適宜置き換えて下さい。

$ mkdir stringutils-happy
$ cd stringutils-happy
$ python -m venv venv
$ source venv/bin/activate  # Windowsでは venv\Scripts\activate

プロジェクト構成

次に、プロジェクトのディレクトリ構造を作成します。

ここのフォルダ構成は適宜置き換えて下さい。

stringutils-happy/
├── stringutils_happy/
│   ├── __init__.py
│   ├── string_utils.py
├── tests/
│   ├── test_string_utils.py
├── setup.py
├── README.md

コード

例として「stringutils_happy」のコードを載せます。

stringutils_happy/string_utils.py

まず、ライブラリのメインコードを書きます。

# stringutils_happy/string_utils.py

def reverse_string(s: str) -> str:
    """文字列を逆にする"""
    return s[::-1]

def toggle_case(s: str) -> str:
    """文字列の大文字小文字を反転する"""
    return s.swapcase()

def count_words(s: str) -> int:
    """文字列の単語をカウントする"""
    return len(s.split())
'''

## stringutils_happy/__init__.py
__init__.pyファイルは、パッケージの初期化を行います。
```python
# stringutils_happy/__init__.py

from .string_utils import reverse_string, toggle_case, count_words

tests/test_string_utils.py

次に、テストを作成します。pytestを使用してテストを行います。

# tests/test_string_utils.py

import pytest
from stringutils_happy import reverse_string, toggle_case, count_words

def test_reverse_string():
    assert reverse_string("hello") == "olleh"

def test_toggle_case():
    assert toggle_case("Hello") == "hELLO"

def test_count_words():
    assert count_words("hello world") == 2

if __name__ == "__main__":
    pytest.main()

パッケージのセットアップ

次に、パッケージの設定を行うためにsetup.pyを作成します。

# setup.py

from setuptools import setup, find_packages

setup(
    name="stringutils-happy",
    version="0.1",
    packages=find_packages(),
    install_requires=[],
    author="あなたの名前",
    author_email="あなたのメールアドレス",
    description="A simple string manipulation library",
    long_description=open("README.md").read(),
    long_description_content_type="text/markdown",
    url="ULR",
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
)

パッケージのビルドと公開

パッケージをビルドし、PyPIに公開します。

パッケージのビルド

まず、必要なツールをインストールします。

$ pip install setuptools wheel

次に、パッケージをビルドします。

$ python setup.py sdist bdist_wheel

PyPIに公開

次に、twineをインストールします。

$ pip install twine

PyPIに公開するためのコマンドを実行します。

$ twine upload dist/*

使い方

stringutils-happyライブラリの使い方は非常に簡単です。以下の例を参考にしてください。

from stringutils_happy import reverse_string, toggle_case, count_words

print(reverse_string("hello"))  # "olleh"
print(toggle_case("Hello World"))  # "hELLO wORLD"
print(count_words("Hello world! This is a test."))  # 5

終わりに

私は上記の手順でライブラリを作成しアップしてみました。
内容は大したものではないですが、とりあえずあげてみようという思いで作ってみました。
よかったらスターを押してあげて下さい!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0