File: helpers.py

package info (click to toggle)
python-pint 0.7.2-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 976 kB
  • ctags: 1,314
  • sloc: python: 8,113; makefile: 165
file content (109 lines) | stat: -rw-r--r-- 3,099 bytes parent folder | download
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# -*- coding: utf-8 -*-

from __future__ import division, unicode_literals, print_function, absolute_import


import doctest
from distutils.version import LooseVersion
import re

from pint.compat import unittest, HAS_NUMPY, HAS_UNCERTAINTIES, NUMPY_VER, PYTHON3


def requires_numpy18():
    if not HAS_NUMPY:
        return unittest.skip('Requires NumPy')
    return unittest.skipUnless(LooseVersion(NUMPY_VER) >= LooseVersion('1.8'), 'Requires NumPy >= 1.8')


def requires_numpy_previous_than(version):
    if not HAS_NUMPY:
        return unittest.skip('Requires NumPy')
    return unittest.skipUnless(LooseVersion(NUMPY_VER) < LooseVersion(version), 'Requires NumPy < %s' % version)


def requires_numpy():
    return unittest.skipUnless(HAS_NUMPY, 'Requires NumPy')


def requires_not_numpy():
    return unittest.skipIf(HAS_NUMPY, 'Requires NumPy is not installed.')


def requires_uncertainties():
    return unittest.skipUnless(HAS_UNCERTAINTIES, 'Requires Uncertainties')


def requires_not_uncertainties():
    return unittest.skipIf(HAS_UNCERTAINTIES, 'Requires Uncertainties is not installed.')


def requires_python2():
    return unittest.skipIf(PYTHON3, 'Requires Python 2.X.')


def requires_python3():
    return unittest.skipUnless(PYTHON3, 'Requires Python 3.X.')


_number_re = '([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)'
_q_re = re.compile('<Quantity\(' + '\s*' + '(?P<magnitude>%s)' % _number_re +
                   '\s*,\s*' + "'(?P<unit>.*)'" + '\s*' + '\)>')

_sq_re = re.compile('\s*' + '(?P<magnitude>%s)' % _number_re +
                    '\s' + "(?P<unit>.*)")

_unit_re = re.compile('<Unit\((.*)\)>')


class PintOutputChecker(doctest.OutputChecker):

    def check_output(self, want, got, optionflags):
        check = super(PintOutputChecker, self).check_output(want, got, optionflags)
        if check:
            return check

        try:
            if eval(want) == eval(got):
                return True
        except:
            pass

        for regex in (_q_re, _sq_re):
            try:
                parsed_got = regex.match(got.replace(r'\\', '')).groupdict()
                parsed_want = regex.match(want.replace(r'\\', '')).groupdict()

                v1 = float(parsed_got['magnitude'])
                v2 = float(parsed_want['magnitude'])

                if abs(v1 - v2) > abs(v1) / 1000:
                    return False

                if parsed_got['unit'] != parsed_want['unit']:
                    return False

                return True
            except:
                pass

        cnt = 0
        for regex in (_unit_re, ):
            try:
                parsed_got, tmp = regex.subn('\1', got)
                cnt += tmp
                parsed_want, temp = regex.subn('\1', want)
                cnt += tmp

                if parsed_got == parsed_want:
                    return True

            except:
                pass

        if cnt:
            # If there was any replacement, we try again the previous methods.
            return self.check_output(parsed_want, parsed_got, optionflags)

        return False