-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment 4_4
35 lines (32 loc) · 1.72 KB
/
Assignment 4_4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'''
A program which generates randomly a number of date and time values and displays them in Finnish language.
'''
import random
import time
import re
#for Generating random date and time
def strTimeProp(start, end, format, prop):
'''Get a time at a proportion of a range of two formatted times.
start and end should be strings specifying times formated in the
given format (strftime-style), giving an interval [start, end].
prop specifies how a proportion of the interval to be taken after
start. The returned time will be in the specified format.'''
stime = time.mktime(time.strptime(start, format))
etime = time.mktime(time.strptime(end, format))
ptime = stime + prop * (etime - stime)
return time.strftime(format, time.localtime(ptime))
def randomDate(start, end, prop):
return strTimeProp(start, end, '%m/%B/%Y %I:%M %p', prop)
a=randomDate("1/January/2008 1:30 PM", "1/January/2009 4:50 AM", random.random())
print(a)
#display the generated date in Finnish language
def multiple_replacer(*key_values):
replace_dict = dict(key_values)
replacement_function = lambda match: replace_dict[match.group(0)]
pattern = re.compile("|".join([re.escape(k) for k, v in key_values]), re.M)
return lambda string: pattern.sub(replacement_function, string)
def multiple_replace(string, *key_values):
return multiple_replacer(*key_values)(string)
replacements = (u"January", u"Tammikuu"), (u"February", u"Helmikuu"), (u"March", u"Maaliskuu"),(u"April", u"Huhtikuu"),(u"May", u"Saattaa"),(u"June", u"Kesäkuu"),
(u"July", u"Heinäkuu"),(u"August", u"Elokuu"),(u"September", u"Syyskuu"),(u"October", u"Lokakuu"),(u"November", u"Marraskuu"),(u"December", u"Joulukuu")
print(multiple_replace(a, *replacements))