This repository was archived by the owner on Sep 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdateconverter.py
More file actions
61 lines (53 loc) · 1.49 KB
/
dateconverter.py
File metadata and controls
61 lines (53 loc) · 1.49 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: UTF-8 -*-
'''
Created on 9 Jul 2012
@author: ramy
'''
import re
class DateConverter:
'''
classdocs
'''
datedict = {
'[Jj]anuary' : 'Jan.',
'[Ff]ebruary' : 'Feb.',
'[Mm]arch' : 'Mar.',
'[Aa]pril' : 'Apr.',
'[Mm]ay' : 'May',
'[Jj]une' : 'June',
'[Jj]uly' : 'July',
'[Aa]ugust' : 'Aug.',
'[Ss]eptember' : 'Sept.',
'[Oo]ctober' : 'Oct.',
'[Nn]ovember' : 'Nov.',
'[Dd]ecember' : 'Dec.',
'[Pp]resent' : 'Now',
'[Jj]anvier' : 'Janv.',
'[Ff]évrier' : 'Févr.',
'[Mm]ars' : 'Mars',
'[Aa]vril' : 'Avril',
'[Mm]ai' : 'Mai',
'[Jj]uin' : 'Juin',
'[Jj]uillet' : 'Juil.',
'[Aa]oût' : 'Août',
'[Ss]eptembre' : 'Sept.',
'[Oo]ctobre' : 'Oct.',
'[Nn]ovembre' : 'Nov.',
'[Dd]écembre' : 'Déc.'
}
def __init__(self):
'''
Constructor
'''
def dict_sub(self, text):
""" Replace in 'text' non-overlapping occurences of REs whose patterns are keys
in dictionary 'd' by corresponding values (which must be constant strings: may
have named backreferences but not numeric ones). The keys must not contain
anonymous matching-groups.
Returns the new string."""
# Create a regular expression from the dictionary keys
regex = re.compile("|".join("(%s)" % k for k in self.datedict))
# Facilitate lookup from group number to value
lookup = dict((i+1, v) for i, v in enumerate(self.datedict.itervalues()))
# For each match, find which group matched and expand its value
return regex.sub(lambda mo: mo.expand(lookup[mo.lastindex]), text)