def __call__(self, lines): for line in lines: line = line.rstrip() stripped = line.strip() if len(stripped) == 0: print('') elif stripped[0] == '#': date = parse_date(stripped[1:].strip()) if self.__first_day: self.__first_day = False else: self.handle_day_end() print('\nDate: %s' % date) else: try: splitted = stripped.split(':') expense = splitted[0] expense = parse_expense(expense) self.handle_expense(expense, ':'.join(splitted[1:])) print('%7.2f : %s' % (expense, ':'.join(splitted[1:]))) except ValueError: print(line) self.handle_day_end() print('\nTotal: %7.2f' % self.__total)
def parse_datetime(datetime_input, output_format='%m/%d/%Y %H:%M:%S', return_datetime=False): """ """ datetime_input = str(datetime_input) case1 = re.compile(r"^(.+)\sat\s(.+)$", re.IGNORECASE) case2 = re.compile(r"^(.+)\son\s(.+)$", re.IGNORECASE) case3 = re.compile(r"^(\d{8})(\d{4,6})$") case4 = re.compile(r"^(\d{9,10})$") match_value = case1.match(datetime_input) if case1.match(datetime_input): match_value = case1.match(datetime_input) date = parse_date(match_value.group(1), '%Y-%m-%d') timestamp = parse_time(match_value.group(2), '%H:%M:%S') elif case2.match(datetime_input): match_value = case2.match(datetime_input) date = parse_date(match_value.group(2), '%Y-%m-%d') timestamp = parse_time(match_value.group(1), '%H:%M:%S') elif case3.match(datetime_input): match_value = case3.match(datetime_input) date = parse_date(match_value.group(1), '%Y-%m-%d') timestamp = parse_time(match_value.group(2), '%H:%M:%S') elif case4.match(datetime_input): match_value = case4.match(datetime_input) date = time.strftime('%Y-%m-%d', time.localtime(float(match_value.group(1)))) timestamp = time.strftime('%H:%M:%S', time.localtime(float(match_value.group(1)))) else: print( "\nERROR[500]: The format that you entered does not match any known datetime formats." ) print(config.parse_datetime_usage) return None datetime_result = datetime.strptime('{} {}'.format(date, timestamp), '%Y-%m-%d %H:%M:%S') if return_datetime: return datetime_result else: return datetime.strftime(datetime_result, output_format)
def coerce_value(self, value): """ coerce the given value to be storable in the current type """ if self.builtin_type == self.builtin_types.Date and type(value) == str: dt = parse_date.parse_date(value) return dt for target in self.py_types[self.builtin_type]: convert = None try: convert = target(value) except: pass if convert is not None: return convert return None
def test_parse_date(input, expected): '''@tests parse_time.parse_time''' if expected is False: assert parse_date.parse_date(input) == False else: assert parse_date.parse_date(input) == expected