|
| 1 | +from typing import List, Optional |
| 2 | +from colorama import Fore |
| 3 | + |
| 4 | + |
| 5 | +class Guitar: |
| 6 | + def __init__(self, name: str = None, price: float = 0.0, |
| 7 | + img: str = None, style: str = None): |
| 8 | + self.style = style |
| 9 | + self.img = img |
| 10 | + self.price = price |
| 11 | + self.name = name |
| 12 | + |
| 13 | + |
| 14 | +def all_guitars(style: Optional[str]) -> List[Guitar]: |
| 15 | + if not style or not style.strip(): |
| 16 | + raise ValueError("'style' cannot be empty.") |
| 17 | + |
| 18 | + style = style.lower() |
| 19 | + |
| 20 | + log(f"Guitars for {style}") |
| 21 | + guitars = get_guitars_from_db() |
| 22 | + if style == 'all': |
| 23 | + return guitars |
| 24 | + |
| 25 | + return [ |
| 26 | + g |
| 27 | + for g in guitars |
| 28 | + if g.style == style |
| 29 | + ] |
| 30 | + |
| 31 | + |
| 32 | +def log(msg: str): |
| 33 | + # raise Exception("NO LOG!") |
| 34 | + print(Fore.YELLOW + "LOGGING THIS TO A FILE, SHOULD NOT SEE IN TEST: " + Fore.WHITE + f"{msg}.") |
| 35 | + |
| 36 | + |
| 37 | +# noinspection DuplicatedCode |
| 38 | +def get_guitars_from_db(): |
| 39 | + # raise Exception("NO DB!") |
| 40 | + print(Fore.YELLOW + "GETTING GUITARS FROM DB! Should not see in test." + Fore.WHITE) |
| 41 | + # This guitar data simulates what we would actually get back from the database. |
| 42 | + guitars = [ |
| 43 | + Guitar('AX Black', 499, '/static/img/guitars/ax-black.jpg', style='electric'), |
| 44 | + Guitar('Jet Black Electric', 599, '/static/img/guitars/jet-black-electric.jpg', style='electric'), |
| 45 | + Guitar('Weezer Classic', 1499, '/static/img/guitars/weezer-classic.jpg', style='electric'), |
| 46 | + Guitar('Acoustic Black', 1299, '/static/img/guitars/black-acoustic.jpg', style='acoustic'), |
| 47 | + Guitar('Mellow Yellow', 799, '/static/img/guitars/mellow-yellow.jpg', style='electric'), |
| 48 | + Guitar('White Vibes', 699, '/static/img/guitars/white-vibes.jpg', style='electric'), |
| 49 | + Guitar('Brush Riffs', 599, '/static/img/guitars/brushed-black-electric.jpg', style='electric'), |
| 50 | + Guitar("Nature's Song", 799, '/static/img/guitars/natures-song.jpg', style='electric'), |
| 51 | + Guitar('Electric Wood Grain', 399, '/static/img/guitars/woodgrain-electric.jpg', style='electric'), |
| 52 | + ] |
| 53 | + guitars.sort(key=lambda g: g.price, reverse=True) |
| 54 | + return guitars |
0 commit comments