[distances] Conditionate the temporal functions by the correct import of dateutil, closes #134762

authorVincent Michel <vincent.michel@logilab.fr>
changeset87a6dcf35dfa
branchdefault
phasepublic
hiddenno
parent revision#f03a4f2a8b04 Added tag nazca-debian-version-0.2.1-1 for changeset 087ed60a535a
child revision#67bb53fb1ed3 preparing 0.2.2
files modified by this revision
distances.py
# HG changeset patch
# User Vincent Michel <vincent.michel@logilab.fr>
# Date 1366790588 -7200
# Wed Apr 24 10:03:08 2013 +0200
# Node ID 87a6dcf35dfadb9041a08235831aaf4b9dc83011
# Parent f03a4f2a8b04d39dea07d81e6707e60353ab34c4
[distances] Conditionate the temporal functions by the correct import of dateutil, closes #134762

diff --git a/distances.py b/distances.py
@@ -13,11 +13,15 @@
1  # details.
2  #
3  # You should have received a copy of the GNU Lesser General Public License along
4  # with this program. If not, see <http://www.gnu.org/licenses/>.
5 
6 -from dateutil import parser as dateparser
7 +try:
8 +    from dateutil import parser as dateparser
9 +    DATEUTIL_ENABLED = True
10 +except ImportError:
11 +    DATEUTIL_ENABLED = False
12  from math import cos, sqrt, pi #Needed for geographical distance
13 
14  from scipy import matrix
15 
16  from nazca.normalize import tokenize
@@ -187,60 +191,61 @@
17      seta = set(tokenize(stra, tokenizer))
18      setb = set(tokenize(strb, tokenizer))
19      return 1.0 - 1.0*len(seta.intersection(setb))/len(seta.union(setb))
20 
21 
22 -### TEMPORAL DISTANCES ########################################################
23 -class FrenchParserInfo(dateparser.parserinfo):
24 -    """ Inherit of the dateutil.parser.parserinfo and translate the english
25 -        dependant variables into french.
26 -    """
27 +if DATEUTIL_ENABLED:
28 +    ### TEMPORAL DISTANCES ####################################################
29 +    class FrenchParserInfo(dateparser.parserinfo):
30 +        """ Inherit of the dateutil.parser.parserinfo and translate the english
31 +            dependant variables into french.
32 +        """
33 
34 -    HMS = [(u'h', u'heure', u'heures'),
35 -           (u'm', u'minute', u'minutes'),
36 -                (u's', u'seconde', u'seconde'),]
37 -    JUMP = [u' ', u'.', u',', u';', u'-', u'/', u"'",
38 -           u'a', u'le', u'et', u'er']
39 -    MONTHS = [(u'Jan', u'Janvier'), (u'Fev', u'Fevrier'),
40 -              (u'Mar', u'Mars'), (u'Avr', u'Avril'), (u'Mai', u'Mai'),
41 -              (u'Jun', u'Juin'), (u'Jui', u'Juillet'),
42 -              (u'Aou', u'Aout'), (u'Sep', u'Septembre'),
43 -              (u'Oct', u'Octobre'), (u'Nov', u'Novembre'),
44 -              (u'Dec', u'Decembre')]
45 -    PERTAIN = [u'de']
46 -    WEEKDAYS = [(u'Lun', u'Lundi'),
47 -                (u'Mar', u'Mardi'),
48 -                (u'Mer', u'Mercredi'),
49 -                (u'Jeu', u'Jeudi'),
50 -                (u'Ven', u'Vendredi'),
51 -                (u'Sam', u'Samedi'),
52 -                (u'Dim', u'Dimanche')]
53 +        HMS = [(u'h', u'heure', u'heures'),
54 +               (u'm', u'minute', u'minutes'),
55 +                    (u's', u'seconde', u'seconde'),]
56 +        JUMP = [u' ', u'.', u',', u';', u'-', u'/', u"'",
57 +               u'a', u'le', u'et', u'er']
58 +        MONTHS = [(u'Jan', u'Janvier'), (u'Fev', u'Fevrier'),
59 +                  (u'Mar', u'Mars'), (u'Avr', u'Avril'), (u'Mai', u'Mai'),
60 +                  (u'Jun', u'Juin'), (u'Jui', u'Juillet'),
61 +                  (u'Aou', u'Aout'), (u'Sep', u'Septembre'),
62 +                  (u'Oct', u'Octobre'), (u'Nov', u'Novembre'),
63 +                  (u'Dec', u'Decembre')]
64 +        PERTAIN = [u'de']
65 +        WEEKDAYS = [(u'Lun', u'Lundi'),
66 +                    (u'Mar', u'Mardi'),
67 +                    (u'Mer', u'Mercredi'),
68 +                    (u'Jeu', u'Jeudi'),
69 +                    (u'Ven', u'Vendredi'),
70 +                    (u'Sam', u'Samedi'),
71 +                    (u'Dim', u'Dimanche')]
72 
73 -def temporal(stra, strb, granularity=u'days', parserinfo=FrenchParserInfo,
74 -             dayfirst=True, yearfirst=False):
75 -    """ Return the distance between two strings (read as dates).
76 +    def temporal(stra, strb, granularity=u'days', parserinfo=FrenchParserInfo,
77 +                 dayfirst=True, yearfirst=False):
78 +        """ Return the distance between two strings (read as dates).
79 
80 -        ``granularity`` can be either ``days`` or ``months`` or ``years``
81 -        (be careful to the plural form !)
82 -        ``language`` can be either french or english
83 +            ``granularity`` can be either ``days`` or ``months`` or ``years``
84 +            (be careful to the plural form !)
85 +            ``language`` can be either french or english
86 
87 -        ``dayfirst`` and ``yearfirst`` are used in case of ambiguity, for
88 -        instance 09/09/09, by default it assumes it's day/month/year
89 +            ``dayfirst`` and ``yearfirst`` are used in case of ambiguity, for
90 +            instance 09/09/09, by default it assumes it's day/month/year
91 
92 -        Neither stra nor strb can have accent. Clean it before.
93 -    """
94 +            Neither stra nor strb can have accent. Clean it before.
95 +        """
96 
97 -    datea = dateparser.parse(stra, parserinfo=parserinfo(dayfirst,
98 -                             yearfirst), fuzzy=True)
99 -    dateb = dateparser.parse(strb, parserinfo=parserinfo(dayfirst,
100 -                             yearfirst), fuzzy=True)
101 -    diff = datea - dateb
102 -    if granularity.lower() == 'years':
103 -        return abs(diff.days/365.25)
104 -    if granularity.lower() == 'months':
105 -        return abs(diff.days/30.5)
106 -    return abs(diff.days)
107 +        datea = dateparser.parse(stra, parserinfo=parserinfo(dayfirst,
108 +                                 yearfirst), fuzzy=True)
109 +        dateb = dateparser.parse(strb, parserinfo=parserinfo(dayfirst,
110 +                                 yearfirst), fuzzy=True)
111 +        diff = datea - dateb
112 +        if granularity.lower() == 'years':
113 +            return abs(diff.days/365.25)
114 +        if granularity.lower() == 'months':
115 +            return abs(diff.days/30.5)
116 +        return abs(diff.days)
117 
118 
119  ### GEOGRAPHICAL DISTANCES ####################################################
120  def geographical(pointa, pointb, in_radians=False, planet_radius=6371009,
121                   units='m'):