python

My Note

  • Max heap (normal one is min heap -> max is hidden)
python
import heapq

heapq._heapify_max(x)
heapq._heappop_max(heap)
heapq._heapreplace_max(heap, item)
  • Rotating log files
python
import logging
from logging.handlers import RotatingFileHandler

handlers = [RotatingFileHandler(filename="app.log", mode="w", maxbytes=512_000, backupCount=4)]

logging.basicConfig(
    handlers=handlers,
    level=logging.DEBUG,
    format="%(levelname)s %(asctime)s %(message)s",
    datefmt="%m/%d/%Y:%I:%M:%S %p",
)

logger = logging.getLogger(__name__)
  • Doc style
python
def factorial(n: int) -> int:
    """
    Compute n! recursively

    :param n: an integer >= 0
    :returns: n!

    Because of Python's stack limitation, this won't compute
    a value larger than 1000!.

    >>> factorial(5)
    120
    """
    if n == 0:
        return 1
    return n * factorial(n - 1)