code: def previous_weekday(day=None, as_datetime=False):
      
      if day is None:
          day = datetime.datetime.now()
      else:
          day = datetime.datetime.strptime(day, '%Y-%m-%d')
  
      day -= datetime.timedelta(days=1)
      while day.weekday() > 4:  # Mon-Fri are 0-4
          day -= datetime.timedelta(days=1)
  
      if as_datetime:
          return day
      return day.strftime("%Y-%m-%d")
docs: get the most recent business day