Skip to content

Instantly share code, notes, and snippets.

@SebDeclercq
Created June 3, 2019 09:05
Show Gist options
  • Save SebDeclercq/72f88adf87aff40de1d6badbab53768e to your computer and use it in GitHub Desktop.
Save SebDeclercq/72f88adf87aff40de1d6badbab53768e to your computer and use it in GitHub Desktop.
Adapted from "Think Python" exercise 3.3
from typing import Optional
class SquareDrawer:
def __init__(self, side_size: int = 5, side_amount: int = 4) -> None:
self._check_params(side_size, side_amount)
def draw_square(
self, side_size: Optional[int] = None, side_amount: Optional[int] = None
) -> None:
self._check_params(side_size, side_amount)
self._print_square_row()
for _ in range(2):
for _ in range(self.nb_chars):
self._print_square_row(sep='|', char=' ')
self._print_square_row()
@property
def nb_squares_by_row(self) -> int:
return int(self.side_amount / 2)
@property
def nb_chars(self) -> int:
return self.side_size - 2
def _check_params(self, side_size: Optional[int], side_amount: Optional[int]) -> None:
self._check_side_size(side_size)
self._check_side_amount(side_amount)
def _check_side_size(self, side_size: Optional[int]) -> None:
if side_size is not None:
if side_size < 1:
raise ValueError(f'"side_size" should be no less than 1')
else:
self.side_size = side_size
def _check_side_amount(self, side_amount: Optional[int]) -> None:
if side_amount is not None:
if side_amount % 2:
raise ValueError(f'"side_amount" should be even')
elif side_amount < 1:
raise ValueError(f'"side_amount" should be no less than 1')
else:
self.side_amount = side_amount
def _print_square_row(self, sep: str = '+', char: str = '-') -> None:
print(f'{sep} ', end='')
print((f'{char} ' * self.nb_chars + sep) * self.nb_squares_by_row)
sd: SquareDrawer = SquareDrawer()
sd.draw_square()
sd.draw_square(5, 10)
sd.draw_square(15, 6)
@SebDeclercq
Copy link
Author

Prints

+ - - - +- - - +
|       |      |
|       |      |
|       |      |
+ - - - +- - - +
|       |      |
|       |      |
|       |      |
+ - - - +- - - +
+ - - - +- - - +- - - +- - - +- - - +
|       |      |      |      |      |
|       |      |      |      |      |
|       |      |      |      |      |
+ - - - +- - - +- - - +- - - +- - - +
|       |      |      |      |      |
|       |      |      |      |      |
|       |      |      |      |      |
+ - - - +- - - +- - - +- - - +- - - +
+ - - - - - - - - - - - - - +- - - - - - - - - - - - - +- - - - - - - - - - - - - +
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
+ - - - - - - - - - - - - - +- - - - - - - - - - - - - +- - - - - - - - - - - - - +
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
|                           |                          |                          |
+ - - - - - - - - - - - - - +- - - - - - - - - - - - - +- - - - - - - - - - - - - +

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment