31df0109128c6248098790e6add11b7ff9faf52d
David Blume Upgrade to Python 3

David Blume authored 4 years ago

1) #!/usr/bin/env python3
David Blume first commit; just disconne...

David Blume authored 8 years ago

2) # "Hello Friends" in Chinese: 朋友你好
3) import os
4) import sys
5) import time
David Blume Add an example decorator.

David Blume authored 4 years ago

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

David Blume authored 8 years ago

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

David Blume authored 8 years ago

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

David Blume authored 4 years ago

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

David Blume authored 4 years ago

11) from decorators import timeit, profile
David Blume Add an example decorator.

David Blume authored 4 years ago

12) 
13) 
David Blume Add return types to the typ...

David Blume authored 4 years ago

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

David Blume authored 8 years ago

16)     """
17)     Defines the function v_print.
18)     It prints if verbose is true, otherwise, it does nothing.
19)     See: http://stackoverflow.com/questions/5980042
20)     :param verbose: A bool to determine if v_print will print its args.
21)     """
22)     global v_print
David Blume Upgrade to Python 3

David Blume authored 4 years ago

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

David Blume authored 8 years ago

24) 
25) 
David Blume Add a Class with a property...

David Blume authored 4 years ago

26) class Coffee:
27) 
28)     def __init__(self, price: float):
29)         self._price = price
30) 
31)     @property
David Blume Addl type hints.

David Blume authored 4 years ago

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

David Blume authored 4 years ago

33)         return self._price
34) 
35)     @price.setter
David Blume Addl type hints.

David Blume authored 4 years ago

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

David Blume authored 4 years ago

37)         if new_price > 0 and isinstance(new_price, float):
38)             self._price = new_price
39)         else:
40)             raise ValueError("price must be a non-negative float.")
41) 
42) 
David Blume Add a profile decorator.

David Blume authored 4 years ago

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

David Blume authored 4 years ago

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

David Blume authored 4 years ago

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

David Blume authored 4 years ago

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

David Blume authored 8 years ago

50)     v_print("Running counter...")
51)     counter.counter.run()
David Blume Add some comments pointing...

David Blume authored 4 years ago

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

David Blume authored 8 years ago

54)     v_print("Running sitesize...")
55)     sitesize.sitesize.run()
David Blume Add some comments pointing...

David Blume authored 4 years ago

56) 
David Blume Add a Class with a property...

David Blume authored 4 years ago

57)     cuppa = Coffee(5.00)
58)     cuppa.price = cuppa.price - 1.00