def _closest_date(target_dt, date_list, before_target=None):
 fb = (lambda d: ((d - target_dt) if (d >= target_dt) else datetime.timedelta.max))
 fa = (lambda d: ((d - target_dt) if (d <= target_dt) else datetime.timedelta.min))
 fnone = (lambda d: ((target_dt - d) if (d < target_dt) else (d - target_dt)))
 if (before_target is None):
  return min(date_list, key=fnone).date()
 if before_target:
  return min(date_list, key=fb).date()
 else:
  return min(date_list, key=fa).date()

def _closest_date ( target_dt , date_list , before_target = None ) : fb = ( lambda d : ( ( d - target_dt ) if ( d >= target_dt ) else datetime . timedelta . max ) ) fa = ( lambda d : ( ( d - target_dt ) if ( d <= target_dt ) else datetime . timedelta . min ) ) fnone = ( lambda d : ( ( target_dt - d ) if ( d < target_dt ) else ( d - target_dt ) ) ) if ( before_target is None ) : return min ( date_list , key = fnone ) . date ( ) if before_target : return min ( date_list , key = fb ) . date ( ) else : return min ( date_list , key = fa ) . date ( )
this function finds the date in a list closest to the target date .