/
GlebBavykin
/
python_sketches
Обзор
Документация
Войти
/
GlebBavykin
/
python_sketches
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
design_patterns/structural/adapter.py
82 строки
2 KB
Gleb Bavykin
fixed mypy errors
10 июл 2026, 17:15
10 июл 2026, 17:15
1646d69
Код
Авторство
О чём код?
import math """ Peg-in-Hole Assembly """ class RoundPeg: """ Compatible Peg with RoundHole """ def __init__(self, radius: float, length: float): self._radius = radius self._length = length @property def radius(self): return self._radius @property def length(self): return self._length class SquarePeg: """ Incompatible Peg with RoundHole """ def __init__(self, width: float, length: float): self._width = width self._length = length @property def width(self): return self._width @property def length(self): return self._length class RoundHole: """ A RoundHole """ def __init__(self, radius: float, height: float): self._radius = radius self._height = height @property def radius(self): return self._radius @property def height(self): return self._height def fits(self, peg: RoundPeg | SquarePeg): if isinstance(peg, RoundPeg): return self.radius > peg.radius and self.height > peg.length else: raise AttributeError class SquarePegAdapter(RoundPeg): """ Adapter to make SquarePeg compatible with RoundHole """ def __init__(self, square_peg: SquarePeg): self._square_peg = square_peg @property def radius(self): return self._square_peg.width * (math.sqrt(2) / 2) @property def length(self): return self._square_peg.length