Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- datetime_re = re.compile("(?P<time>(?P<length>\d+)\s*(?P<unit>\w)\s*)")
- ages = ['6W', '4M3W', '6Y', '1Y 3M', '16 Y 5M 2 W']
- TIME_PER_YEAR = {
- 'Y': 1.0,
- 'W': 52.0,
- 'M': 12.0
- }
- def age_to_years(length, unit_time):
- try:
- length = int(length)
- except ValueError:
- pass
- return length / TIME_PER_YEAR[unit_time.upper()]
- if __name__ == '__main__':
- for time_input in ages:
- print(time_input)
- for datetime_out in datetime_re.finditer(time_input):
- print(datetime_out.groups())
- print(sum(age_to_years(*datetime_out.groups()[1:]) for datetime_out in datetime_re.finditer(time_input)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement