demo.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import logging
  2. import time
  3. from typing import NamedTuple
  4. from .sampling import OVERHEAD_SAMPLING
  5. from .utils import toolkit
  6. from .. import alive_bar
  7. from ..styles import BARS
  8. from ..utils.colors import BOLD, ORANGE_IT
  9. class Case(NamedTuple):
  10. name: str = None
  11. count: int = None
  12. config: dict = None
  13. done: bool = None
  14. hooks: bool = None
  15. title: str = None
  16. def title(text):
  17. print(f'=== {BOLD.color_code}{ORANGE_IT(text)} ===')
  18. cases = [
  19. Case(title='Definite/unknown modes'),
  20. Case('Normal+total', 1000, dict(total=1000)),
  21. Case('Underflow+total', 800, dict(total=1200)),
  22. Case('Overflow+total', 1200, dict(total=800)),
  23. Case('Unknown', 1000, dict(total=0)),
  24. Case(title='Manual modes'),
  25. Case('Normal+total+manual', 1000, dict(total=1000, manual=True)),
  26. Case('Underflow+total+manual', 800, dict(total=1200, manual=True)),
  27. Case('Overflow+total+manual', 1200, dict(total=800, manual=True)),
  28. Case('Unknown+manual', 1000, dict(total=0, manual=True)),
  29. Case(title='Print and Logging hooks'),
  30. Case('Simultaneous', 1000, dict(total=1000), hooks=True),
  31. # title('Quantifying mode') # soon, quantifying mode...
  32. # ('Calculating auto', 1000, dict(total=..., manual=False)),
  33. # ('Calculating manual', 1000, dict(total=..., manual=True)),
  34. Case(title='Display features'),
  35. Case('Styles', 1000, dict(total=1000, bar='halloween', spinner='loving'))
  36. ]
  37. features = [dict(total=1000, bar=bar, spinner='loving') for bar in BARS]
  38. cases += [Case(name.capitalize(), 1000, {**features[i % len(BARS)], **config}, done=True)
  39. for i, (name, config) in enumerate(OVERHEAD_SAMPLING, 1)]
  40. def demo(sleep=None):
  41. logging.basicConfig(level=logging.INFO)
  42. logger = logging.getLogger(__name__)
  43. for case in cases:
  44. if case.title:
  45. print()
  46. title(case.title)
  47. continue
  48. manual, total = (case.config.get(x) for x in ('manual', 'total'))
  49. with alive_bar(title_length=22, title=case.name, **case.config) as bar:
  50. # bar.text('Quantifying...')
  51. # time.sleep(0)
  52. bar.text('Processing...')
  53. time.sleep(0)
  54. # bar.reset(total)
  55. for i in range(1, case.count + 1):
  56. time.sleep(sleep or .003)
  57. if manual:
  58. bar(float(i) / (total or case.count))
  59. else:
  60. bar()
  61. if case.hooks and i:
  62. if i == 400:
  63. print('nice hassle-free print hook!') # tests hook manager.
  64. elif i == 800:
  65. logger.info('and even logging hook!!!') # tests hook manager.
  66. if case.done:
  67. bar.text('Ok, done!')
  68. if __name__ == '__main__':
  69. parser, run = toolkit('Demonstrates alive-progress, showcasing several common scenarios.')
  70. parser.add_argument('sleep', type=float, nargs='?', help='the sleep time (default=.003)')
  71. run(demo)