Advertisement
cooperlees

6.9th Work Anniversary

Dec 2nd, 2019
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.85 KB | None | 0 0
  1. [cooper:~/random_repo]$ cat experimental/cooper/fv_6_9_th/fv_six_point_nine.py
  2. #!/usr/bin/env python3
  3.  
  4. import asyncio
  5. import logging
  6. import sys
  7. from datetime import datetime, timedelta
  8. from typing import Union
  9.  
  10. import click
  11.  
  12.  
  13. LOG = logging.getLogger(__name__)
  14.  
  15.  
  16. def _handle_debug(
  17.     ctx: click.core.Context,
  18.     param: Union[click.core.Option, click.core.Parameter],
  19.     debug: Union[bool, int, str],
  20. ) -> Union[bool, int, str]:
  21.     """Turn on debugging if asked otherwise INFO default"""
  22.     log_level = logging.DEBUG if debug else logging.INFO
  23.     logging.basicConfig(
  24.         format="[%(asctime)s] %(levelname)s: %(message)s (%(filename)s:%(lineno)d)",
  25.         level=log_level,
  26.     )
  27.     return debug
  28.  
  29.  
  30. async def async_main(debug: bool, start_date: str) -> int:
  31.     year = int(start_date[0:4])
  32.     month = int(start_date[4:6])
  33.     day = int(start_date[6:8])
  34.     if debug:
  35.         print(f"Start Date - Year: {year} Month: {month} Day: {day}")
  36.  
  37.     start_datetime = datetime(year, month, day)
  38.     ninty_pct_days = int(0.9 * 365.0)
  39.     # Not caring about leaps seconds etc. - We plus 1 as there should be 1 leap lear
  40.     six_point_nine_years = timedelta(days=(6 * 365 + ninty_pct_days + 1))
  41.  
  42.     print(
  43.         f"With a start date of {start_date} the 6.9 FV is around:\n"
  44.         + f"{start_datetime + six_point_nine_years}"
  45.     )
  46.     return 0
  47.  
  48.  
  49. @click.command(context_settings={"help_option_names": ["-h", "--help"]})
  50. @click.option(
  51.     "--debug",
  52.     is_flag=True,
  53.     callback=_handle_debug,
  54.     show_default=True,
  55.     help="Turn on debug logging",
  56. )
  57. @click.argument("start_date")
  58. @click.pass_context
  59. def main(ctx: click.core.Context, **kwargs) -> None:
  60.     """ START_DATE needs to be YYYYMMdd """
  61.     LOG.debug(f"Starting {sys.argv[0]}")
  62.     ctx.exit(asyncio.run(async_main(**kwargs)))
  63.  
  64.  
  65. if __name__ == "__main__":
  66.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement