9aa48ca592065f47706148583c8080a446d5c57a
David Blume Upgrade to Python 3

David Blume authored 3 years ago

1) #!/usr/bin/env python3
dblume Add credits.

dblume authored 1 year ago

2) """An example Python project to use for testing."""
3) 
David Blume first commit; just disconne...

David Blume authored 7 years ago

4) import os
5) import sys
6) import time
David Blume Add an example decorator.

David Blume authored 3 years ago

7) import functools
David Blume first commit; just disconne...

David Blume authored 7 years ago

8) from argparse import ArgumentParser
David Blume Compatibility with Python 2.

David Blume authored 7 years ago

9) import counter.counter
10) import sitesize.sitesize
David Blume Add return types to the typ...

David Blume authored 3 years ago

11) from typing import Optional, Callable
David Blume Add a profile decorator.

David Blume authored 3 years ago

12) from decorators import timeit, profile
David Blume Add a dataclass and dataval...

David Blume authored 3 years ago

13) from pathlib import Path
14) try:
15)     import marshmallow
16)     marshmallow_imported = True
17)     import bored.bored
18) except ModuleNotFoundError:
19)     marshmallow_imported = False
David Blume Add an example decorator.

David Blume authored 3 years ago

20) 
dblume Add credits.

dblume authored 1 year ago

21) __author__ = "David Blume"
22) __copyright__ = "Copyright 2016-2022, David Blume"
23) __license__ = "MIT"
24) __version__ = "1.0"
25) __status__ = "Development"
26) 
David Blume Add return types to the typ...

David Blume authored 3 years ago

27) v_print:Callable
28) def set_v_print(verbose: bool) -> None:
David Blume first commit; just disconne...

David Blume authored 7 years ago

29)     """
30)     Defines the function v_print.
31)     It prints if verbose is true, otherwise, it does nothing.
32)     See: http://stackoverflow.com/questions/5980042
33)     :param verbose: A bool to determine if v_print will print its args.
34)     """
35)     global v_print
David Blume Upgrade to Python 3

David Blume authored 3 years ago

36)     v_print = print if verbose else lambda *a, **k: None
David Blume first commit; just disconne...

David Blume authored 7 years ago

37) 
38) 
David Blume Add a dataclass and dataval...

David Blume authored 3 years ago

39) # This class demonstrates decorators.
40) #
41) # Consider these alternatives for data classes:
42) # * dataclasses.dataclass: sensible defaults, compares class type, can be frozen
43) #                          See "bored" module for data validation with marshmallow
44) # * collections.namedtuple: esp. useful for results of csv and sqlite3
David Blume Add a Class with a property...

David Blume authored 3 years ago

45) class Coffee:
46) 
47)     def __init__(self, price: float):
dblume Add note about calling supe...

dblume authored 1 year ago

48)         super().__init__()  # See https://eugeneyan.com/writing/uncommon-python/
David Blume Add a Class with a property...

David Blume authored 3 years ago

49)         self._price = price
50) 
51)     @property
David Blume Addl type hints.

David Blume authored 3 years ago

52)     def price(self) -> float:
David Blume Add a Class with a property...

David Blume authored 3 years ago

53)         return self._price
54) 
55)     @price.setter
David Blume Addl type hints.

David Blume authored 3 years ago

56)     def price(self, new_price: float) -> None:
David Blume Add a Class with a property...

David Blume authored 3 years ago

57)         if new_price > 0 and isinstance(new_price, float):
58)             self._price = new_price
59)         else:
60)             raise ValueError("price must be a non-negative float.")
61) 
62) 
David Blume Add a profile decorator.

David Blume authored 3 years ago

63) @timeit.timeit
64) @profile.profile  # may invoke with parameters, too
David Blume Add return types to the typ...

David Blume authored 3 years ago

65) def main(debug: bool) -> None:
David Blume Add a tip about setting the...

David Blume authored 3 years ago

66)     script_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
David Blume Add some comments pointing...

David Blume authored 3 years ago

67)     print(f'{sys.argv[0]} is in {script_dir}.')
68) 
69)     # Raymond Hettinger's example use of queue.Queue and threading.Thread
David Blume Compatibility with Python 2.

David Blume authored 7 years ago

70)     v_print("Running counter...")
71)     counter.counter.run()
David Blume Add some comments pointing...

David Blume authored 3 years ago

72) 
73)     # Raymond Hettinger's example use of multiprocessing.pool's ThreadPool
David Blume Compatibility with Python 2.

David Blume authored 7 years ago

74)     v_print("Running sitesize...")
75)     sitesize.sitesize.run()
David Blume Add some comments pointing...

David Blume authored 3 years ago

76) 
David Blume Add a dataclass and dataval...

David Blume authored 3 years ago

77)     # Jessica Chen Fan's validation with dataclasses, marshmallow and desert
78)     if marshmallow_imported:
79)         print(bored.bored.get_activity())
80) 
81)     if (Path.home() / 'bin').exists():
82)         print('Your home directory has a bin/ directory.')
83) 
David Blume Add a Class with a property...

David Blume authored 3 years ago

84)     cuppa = Coffee(5.00)
85)     cuppa.price = cuppa.price - 1.00
dblume Add note about calling supe...

dblume authored 1 year ago

86)     print(f'Coffee on sale for ${cuppa.price:1.2f}.')