File: utils.py

package info (click to toggle)
parsedatetime 2.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 568 kB
  • sloc: python: 5,161; makefile: 44
file content (72 lines) | stat: -rw-r--r-- 2,479 bytes parent folder | download | duplicates (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
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
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
"""
Internal helper functions for unit tests of parsedatetime
"""
from __future__ import unicode_literals


def assertEqualWithComparator(comparator):
    """
    Fail a little less cryptically that unittest.assertTrue when comparing a
    result against a target value. Shows the result and the target in the
    failure message.
    """

    def decoratedComparator(self, result, check, errMsg=None, **kwargs):
        errMsg = errMsg or 'Result does not match target value'
        equal = comparator(self, result, check, **kwargs)
        failureMessage = ('%s\n\n\t'
                          'Result:\n\t%s\n\n\tExpected:\n\t%s')

        if not equal:
            self.fail(failureMessage % (errMsg, result, check))

    return decoratedComparator


def compareResultByTimeTuplesAndFlags(result, check, dateOnly=False):
    """
    Ensures that flags are an exact match and time tuples a close match when
    given data in the format ((timetuple), flag)
    """
    return (_compareTimeTuples(result[0], check[0], dateOnly) and
            _compareFlags(result[1], check[1]))


def compareResultByFlags(result, check, dateOnly=False):
    """
    Ensures that flags are an exact match when given data in the format
    ((timetuple), flag)
    """
    return _compareFlags(result[1], check[1])


def compareResultByTimeTupleRangesAndFlags(result, check, dateOnly=False):
    """
    Ensures that flags are an exact match and time tuples a close match when
    given data in the format ((timetuple), (timetuple), flag)
    """
    return (_compareTimeTuples(result[0], check[0], dateOnly) and
            _compareTimeTuples(result[1], check[1], dateOnly) and
            _compareFlags(result[2], check[2]))


def _compareTimeTuples(target, value, dateOnly=False):
    """
    Ignores minutes and seconds as running the test could cross a minute
    boundary. Technically the year, month, day, hour, minute, and second could
    all change if the test is run on New Year's Eve, but we won't worry about
    less than per-hour granularity.
    """
    t_yr, t_mth, t_dy, t_hr, t_min, _, _, _, _ = target
    v_yr, v_mth, v_dy, v_hr, v_min, _, _, _, _ = value

    if dateOnly:
        return ((t_yr == v_yr) and (t_mth == v_mth) and (t_dy == v_dy))
    else:
        return ((t_yr == v_yr) and (t_mth == v_mth) and (t_dy == v_dy) and
                (t_hr == v_hr) and (t_min == v_min))


def _compareFlags(result, check):
    return (result == check)