Fastapi

Chapter 3-4 Base62 디버깅과 디버깅 스킬의 중요성

Chansman 2025. 5. 27. 10:05

🧪 Base62 디버깅과 디버깅 스킬의 중요성

1. 🎯 Base62 디버깅 개요

  • Base62는 고유한 식별자를 짧고 URL-safe하게 만들기 위한 인코딩 방식입니다.
  • 디버깅을 통해 그 내부 동작 원리를 확인하고 이해도를 높일 수 있습니다.

2. ⚠️ PyCharm 디버깅 에러 주의

AttributeError: '_MainThread' object has no attribute 'isAlive'. Did you mean: 'is_alive'?

3. 💡 디버깅의 중요성

"개발자는 코딩보다 디버깅이다" — 김포프

  • 디버깅을 통해 코드를 논리적으로 추론하고 설계까지 이해할 수 있습니다.
  • 디버깅을 잘하면 남의 코드도 두렵지 않고, 실력도 자연스럽게 향상됩니다.

4. 🔍 Base62 디버깅 예제 코드

import string
from typing import Final, ClassVar

class Base62:
    BASE: Final[ClassVar[str]] = string.ascii_letters + string.digits
    BASE_LEN: Final[ClassVar[int]] = len(BASE)

    @classmethod
    def encode(cls, num: int) -> str:
        if num < 0:
            raise ValueError(f"{cls}.encode() needs positive integer but you passed: {num}")

        if num == 0:
            return cls.BASE[0]

        result = []
        while num:
            num, remainder = divmod(num, cls.BASE_LEN)
            result.append(cls.BASE[remainder])
        return "".join(result)

print(Base62.encode(62))  # "ab"
print(Base62.encode(124))  # "ac"

5. 🐞 디버깅 기초 조작법 (PyCharm 기준)

  • 중단점(Breakpoint): 실행할 코드 줄 번호 클릭
  • Debug 실행: 벌레 모양 클릭
  • Step Over: 한 줄 실행
  • Step Into: 함수 내부로 진입
  • Step Out: 호출한 함수로 복귀
  • Evaluate Expression: 변수나 식의 결과 직접 확인 가능

자세한 설명: https://www.jetbrains.com/help/pycharm/stepping-through-the-program.html


6. ⚙️ Deterministic 연산이란?

  • 결정적(deterministic): 같은 입력에 대해 항상 같은 결과를 내는 연산
  • Base62는 deterministic 방식
    • ex) Base62.encode(62) -> 항상 "ab"

7. 🧬 고유 ID 생성 대안: sqids

📄 app/utils/squids.py

import random
from datetime import datetime
import sqids

sqid = sqids.Sqids()

class Squids:
    @classmethod
    def encode(cls, nums: list[int]) -> str:
        return sqid.encode(nums)

if __name__ == '__main__':
    now = datetime.now()
    print(Squids.encode([
        now.year, now.month, now.day,
        now.hour, now.minute, now.second,
        now.microsecond, random.randint(1, 9)
    ]))
  • 시간 기반 + 랜덤 수 조합 → 충분히 유일한 식별자 생성
  • URL-safe 문자로 구성됨
  • 공식 문서: https://sqids.org/ko

🧠 요약

항목 내용

Base62 URL-safe 식별자 생성용 인코딩 방식
디버깅 중요성 개발 실력의 핵심 스킬
PyCharm 팁 최신 버전 유지, Evaluate 사용
Deterministic 동일 입력 → 동일 출력
Sqids 대안 식별자 생성 방식 (시간 기반 + 랜덤)