Advertisement
DeaD_EyE

strange dates

Mar 7th, 2022
1,243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. from dataclasses import dataclass, field
  2. from datetime import datetime as Date
  3.  
  4. from rich import get_console
  5. from rich.table import Table
  6.  
  7.  
  8. @dataclass
  9. class War:
  10.     name: str
  11.     entry_date: Date
  12.  
  13.     @property
  14.     def date_sum(self):
  15.         day = self.entry_date.day
  16.         month = self.entry_date.month
  17.         century = self.entry_date.year // 100
  18.         decade = self.entry_date.year % 100
  19.         return day + month + century + decade
  20.  
  21.     @property
  22.     def row(self):
  23.         return self.name, self.entry_date.strftime("%Y-%m-%d"), str(self.date_sum)
  24.  
  25.  
  26. wars = [
  27.     War("World War 1", Date(1914, 7, 28)),
  28.     War("World War 2", Date(1939, 9, 1)),
  29.     War('"Official" Ukraine War', Date(2022, 2, 24)),
  30. ]
  31.  
  32. console = get_console()
  33. table = Table()
  34. table.add_column("Name")
  35. table.add_column("Entry date")
  36. table.add_column("Date Sum")
  37.  
  38. for war in wars:
  39.     table.add_row(*war.row)
  40.  
  41.  
  42. console.print(table)
  43.  
  44. # output
  45. """
  46. ┏━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━┓
  47. ┃ Name                   ┃ Entry date ┃ Date Sum ┃
  48. ┡━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━┩
  49. │ World War 1            │ 1914-07-28 │ 68       │
  50. │ World War 2            │ 1939-09-01 │ 68       │
  51. │ "Official" Ukraine War │ 2022-02-24 │ 68       │
  52. └────────────────────────┴────────────┴──────────┘
  53. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement