Workspace
Damien Clarke/

Are We There Yet?

0
Beta
Spinner
from datetime import timedelta, date,datetime
from dateutil import relativedelta
import logging 
logging.basicConfig(filename='example.log', 
									encoding='utf-8', 
									level=logging.DEBUG,
									format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') 
 
dates_logger = logging.getLogger('dates')
class AreWeThereYet:   
    """
    derives interval in months, weeks and and days
    """
    class Dates:
        """
        Class uses a heading and date data to derive interval and customised output string. 
        """
        
        def __init__(self,heading:str,target_date:str):
         """
            Class uses the heading and ISO formatted date string to derive the parameters for the message string

            Parameters
            ----------
            heading : str
                description of date.
            target_date : str
                date string.

            Returns
            -------
            heading : str 
            	description of date.
            target_date: date
            	date string.
            target_days: int
            	interval in days.
            target_weeks: int
            	interval in weeks.
            delta_months: int
            	whole months of interval.
            delta_weeks: int
            	whole weeks of interval less whole months.
            delta_days: int
            	whole days of days less months and weeks.
            msg : str
            	formatted string woyh parameters
            """
         self.target_date = date.fromisoformat(target_date)
         self.heading = heading
         target_data = self.target_date - date.today()
         self.target_days = target_data.days
            
         self.target_weeks = self.target_days//7
         delta_to_target = relativedelta.relativedelta(
                self.target_date,date.today())
          #self.target_weeks = delta_to_target.days % 7  
         self.delta_months = delta_to_target.months
         self.delta_weeks = delta_to_target.weeks
         self.delta_days = delta_to_target.days % 7
         msg = "* {} *\n{} days or {} weeks total\n".format(self.heading,self.target_days, self.target_weeks)
         msg = msg + self.__output_format(self.delta_months, ["month","months"], False)
         msg = msg + self.__output_format(self.delta_weeks,["week", "weeks"], prefix = True)
         
         msg = msg + self.__output_format(self.delta_days ,["day","days"],True)
                	
         self.msg = msg + " to {} from today {}\n".format(
                   datetime.strftime(self.target_date,'%d-%m-%Y'),date.strftime(
                       date.today(),'%d-%m-%Y'))
         if self.msg[:1] == ", ":
                self.msg = self.msg[1:]
 		             
                       
        def __output_format(self, data : int, units : list, prefix: bool ):
            if prefix:
            	comma = ", "
            else:
             	comma = ""
            if data:
               if data == 1:
               	unit = units[0]
               else:
               	unit = units[1]
               return "{}{} {}".format(comma,data, unit)
            return ""
            
    class Batch:
        """
        """
        
        def __init__(self,data:dict):
            self.data = data
 		
        def process(self):
            """
            

            Returns
            -------
            msgs : TYPE
                DESCRIPTION.

            """
            msgs = ""
            for item in self.data.items():
                my_dates = AreWeThereYet.Dates(*item)
                msgs = msgs + my_dates.msg
            return msgs
Hidden code
  • AI Chat
  • Code