__main__.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from __future__ import annotations
  2. from prettytable import PrettyTable, TableStyle
  3. from prettytable.colortable import ColorTable, Theme, Themes
  4. FIELD_NAMES = ["City name", "Area", "Population", "Annual Rainfall"]
  5. ROWS = [
  6. ["Adelaide", 1295, 1158259, 600.5],
  7. ["Brisbane", 5905, 1857594, 1146.4],
  8. ["Darwin", 112, 120900, 1714.7],
  9. ["Hobart", 1357, 205556, 619.5],
  10. ["Melbourne", 1566, 3806092, 646.9],
  11. ["Perth", 5386, 1554769, 869.4],
  12. ["Sydney", 2058, 4336374, 1214.8],
  13. ]
  14. if __name__ == "__main__":
  15. table = PrettyTable()
  16. table.field_names = FIELD_NAMES
  17. for row in ROWS:
  18. if row[0] == "Hobart":
  19. table.add_row(row, divider=True)
  20. else:
  21. table.add_row(row)
  22. for style in TableStyle:
  23. print("PrettyTable style:", style.name)
  24. print()
  25. table.set_style(style)
  26. print(table)
  27. print()
  28. table = ColorTable()
  29. table.field_names = FIELD_NAMES
  30. for row in ROWS:
  31. if row[0] == "Hobart":
  32. table.add_row(row, divider=True)
  33. else:
  34. table.add_row(row)
  35. for name, theme in vars(Themes).items():
  36. if isinstance(theme, Theme):
  37. print("ColorTable theme:", name)
  38. print()
  39. table.theme = theme
  40. print(table)
  41. print()