Skip to content

Latest commit

 

History

History
140 lines (107 loc) · 4.03 KB

README.md

File metadata and controls

140 lines (107 loc) · 4.03 KB

Python

기본적으로 PEP8의 스타일을 따릅니다.

코드 스타일 도구

일관된 스타일을 작성하는데 도움을 주는 다양한 도구들을 활용하고 있습니다.

black

위의 여러 가지 도구중에 가장 중심이 되는 것은 black입니다. black에서 기본값으로 지정된 설정을 따르고 있습니다. black에서 만들어주는 코드의 스타일은 이곳에서 확인할 수 있습니다.

isort

black & isort

[tool.isort]
profile = "black"

flake8

black & flake8

max-line-length = 88
extend-ignore = E203

Type Annotation

타입 힌트의 활용은 코드 작성자 재량에 맡깁니다.

Do

def show_movie(name: str) -> None:
    print("Hello " + name)


def show_movie(name: str) -> None:
    if not name:
        return
    print("Hello " + name)

이외의 규칙

Module Level Dunder

모듈 레벨의 던더(__all__, __version__ 등)들은 모듈 Docstring 하단에 그리고 일반적인 모듈 임포트 위에 위치하도록 합니다.

"""Module Docstring Sample

Blah Blah
"""

__all__ = ['A', 'B', 'C']

import sys
import os

Class

클래스를 정의할 때는 최상위 클래스 object 상속을 명시하지 않고 생략합니다.

Do

class Movie:
    pass

Don't

class Movie(object):
    pass

Comments

Google Python Style Guide의 룰을 따릅니다 .

def fetch_smalltable_rows(
    table_handle: smalltable.Table,
    keys: Sequence[bytes | str],
    require_all_keys: bool = False,
) -> Mapping[bytes, tuple[str, ...]]:
    """Fetches rows from a Smalltable.

    Retrieves rows pertaining to the given keys from the Table instance
    represented by table_handle.  String keys will be UTF-8 encoded.

    Args:
        table_handle: An open smalltable.Table instance.
        keys: A sequence of strings representing the key of each table
          row to fetch.  String keys will be UTF-8 encoded.
        require_all_keys: If True only rows with values set for all keys will be
          returned.

    Returns:
        A dict mapping keys to the corresponding table row data
        fetched. Each row is represented as a tuple of strings. For
        example:

        {b'Serak': ('Rigel VII', 'Preparer'),
         b'Zim': ('Irk', 'Invader'),
         b'Lrrr': ('Omicron Persei 8', 'Emperor')}

        Returned keys are always bytes.  If a key from the keys argument is
        missing from the dictionary, then that row was not found in the
        table (and require_all_keys must have been False).

    Raises:
        IOError: An error occurred accessing the smalltable.
    """

f-string with equal sign

로깅 등의 목적으로 사용될 때, 필요한 경우 등호를 활용합니다. 다만, 다음 경우에 대해서는 직접 포맷팅을 합니다.

  • 구분자로 등호가 아닌 기호를 사용해야하는 경우
  • 평가되는 식과 다른 내용으로 header를 적어야 할 때

Do

logger.info(f"{name = }, {description = }")

Don't do

logger.info(f"name = {name!r}, description = {description!r}")

Other case

logger.info(f"name: {name!r}, description: {description!r}") # 등호 대신 쌍점 사용이 필요한 경우
logger.info(f"{a} + {b} = {a + b}")  # 이름이 아닌 실제 값이 쓰여야 해서 {a + b = }로 대체할 수 없는 경우
logger.info(f"result = {a + b}")  # 식(a + b) 대신 별도의 이름이 필요한 경우