Debian Codegraph @

lib/ansible/plugins/test/mathstuff.py

raw
1 # (c) 2016, Ansible, Inc
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import annotations
19
20 import math
21
22
23 def issubset(a, b):
24 return set(a) <= set(b)
25
26
27 def issuperset(a, b):
28 return set(a) >= set(b)
29
30
31 def isnotanumber(x):
32 try:
33 return math.isnan(x)
34 except TypeError:
35 return False
36
37
38 def contains(seq, value):
39 """Opposite of the ``in`` test, allowing use as a test in filters like ``selectattr``
40
41 .. versionadded:: 2.8
42 """
43 return value in seq
44
45
46 class TestModule:
47 """ Ansible math jinja2 tests """
48
49 def tests(self):
50 return {
51 # set theory
52 'subset': issubset,
53 'issubset': issubset,
54 'superset': issuperset,
55 'issuperset': issuperset,
56 'contains': contains,
57
58 # numbers
59 'nan': isnotanumber,
60 'isnan': isnotanumber,
61 }