#!/usr/bin/env python3 """An example Python project to use for testing.""" import os import sys import time import functools from argparse import ArgumentParser import counter.counter import sitesize.sitesize from typing import Optional, Callable from decorators import timeit, profile from pathlib import Path try: import marshmallow marshmallow_imported = True import bored.bored except ModuleNotFoundError: marshmallow_imported = False __author__ = "David Blume" __copyright__ = "Copyright 2016-2022, David Blume" __license__ = "MIT" __version__ = "1.0" __status__ = "Development" v_print:Callable def set_v_print(verbose: bool) -> None: """ Defines the function v_print. It prints if verbose is true, otherwise, it does nothing. See: http://stackoverflow.com/questions/5980042 :param verbose: A bool to determine if v_print will print its args. """ global v_print v_print = print if verbose else lambda *a, **k: None # This class demonstrates decorators. # # Consider these alternatives for data classes: # * dataclasses.dataclass: sensible defaults, compares class type, can be frozen # See "bored" module for data validation with marshmallow # * collections.namedtuple: esp. useful for results of csv and sqlite3 class Coffee: def __init__(self, price: float): super().__init__() # See https://eugeneyan.com/writing/uncommon-python/ self._price = price @property def price(self) -> float: return self._price @price.setter def price(self, new_price: float) -> None: if new_price > 0 and isinstance(new_price, float): self._price = new_price else: raise ValueError("price must be a non-negative float.") @timeit.timeit @profile.profile # may invoke with parameters, too def main(debug: bool) -> None: script_dir = os.path.abspath(os.path.dirname(sys.argv[0])) print(f'{sys.argv[0]} is in {script_dir}.') # Raymond Hettinger's example use of queue.Queue and threading.Thread v_print("Running counter...") counter.counter.run() # Raymond Hettinger's example use of multiprocessing.pool's ThreadPool v_print("Running sitesize...") sitesize.sitesize.run() # Jessica Chen Fan's validation with dataclasses, marshmallow and desert if marshmallow_imported: print(bored.bored.get_activity()) if (Path.home() / 'bin').exists(): print('Your home directory has a bin/ directory.') cuppa = Coffee(5.00) cuppa.price = cuppa.price - 1.00 print(f'Coffee on sale for ${cuppa.price:1.2f}.') if __name__ == '__main__': parser = ArgumentParser(description='Just a template sample.') parser.add_argument('-d', '--debug', action='store_true') parser.add_argument('-v', '--verbose', action='store_true') args = parser.parse_args() set_v_print(args.verbose) main(args.debug)