From 8872bba21f9593edeedd4672f9744a0b8f78d7ac Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 18 Feb 2015 20:03:05 -0500 Subject: [PATCH] rearranged math filters --- lib/ansible/runner/filter_plugins/core.py | 54 ----------------- .../filter_plugins/{math.py => mathstuff.py} | 59 ++++++++++++++++++- test/units/TestFilters.py | 5 +- 3 files changed, 60 insertions(+), 58 deletions(-) rename lib/ansible/runner/filter_plugins/{math.py => mathstuff.py} (56%) diff --git a/lib/ansible/runner/filter_plugins/core.py b/lib/ansible/runner/filter_plugins/core.py index 4680cf6b58a..4507d45a5d6 100644 --- a/lib/ansible/runner/filter_plugins/core.py +++ b/lib/ansible/runner/filter_plugins/core.py @@ -23,7 +23,6 @@ import types import pipes import glob import re -import collections import crypt import hashlib import string @@ -182,51 +181,6 @@ def ternary(value, true_val, false_val): else: return false_val -def unique(a): - if isinstance(a,collections.Hashable): - c = set(a) - else: - c = [] - for x in a: - if x not in c: - c.append(x) - return c - -def intersect(a, b): - if isinstance(a,collections.Hashable) and isinstance(b,collections.Hashable): - c = set(a) & set(b) - else: - c = unique(filter(lambda x: x in b, a)) - return c - -def difference(a, b): - if isinstance(a,collections.Hashable) and isinstance(b,collections.Hashable): - c = set(a) - set(b) - else: - c = unique(filter(lambda x: x not in b, a)) - return c - -def symmetric_difference(a, b): - if isinstance(a,collections.Hashable) and isinstance(b,collections.Hashable): - c = set(a) ^ set(b) - else: - c = unique(filter(lambda x: x not in intersect(a,b), union(a,b))) - return c - -def union(a, b): - if isinstance(a,collections.Hashable) and isinstance(b,collections.Hashable): - c = set(a) | set(b) - else: - c = unique(a + b) - return c - -def min(a): - _min = __builtins__.get('min') - return _min(a); - -def max(a): - _max = __builtins__.get('max') - return _max(a); def version_compare(value, version, operator='eq', strict=False): ''' Perform a version comparison on a value ''' @@ -386,14 +340,6 @@ class FilterModule(object): 'ternary': ternary, # list - 'unique' : unique, - 'intersect': intersect, - 'difference': difference, - 'symmetric_difference': symmetric_difference, - 'union': union, - 'min' : min, - 'max' : max, - # version comparison 'version_compare': version_compare, diff --git a/lib/ansible/runner/filter_plugins/math.py b/lib/ansible/runner/filter_plugins/mathstuff.py similarity index 56% rename from lib/ansible/runner/filter_plugins/math.py rename to lib/ansible/runner/filter_plugins/mathstuff.py index 7f6cc195556..88109832a0f 100644 --- a/lib/ansible/runner/filter_plugins/math.py +++ b/lib/ansible/runner/filter_plugins/mathstuff.py @@ -15,11 +15,56 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . -from __future__ import absolute_import - import math +import collections from ansible import errors +def unique(a): + if isinstance(a,collections.Hashable): + c = set(a) + else: + c = [] + for x in a: + if x not in c: + c.append(x) + return c + +def intersect(a, b): + if isinstance(a,collections.Hashable) and isinstance(b,collections.Hashable): + c = set(a) & set(b) + else: + c = unique(filter(lambda x: x in b, a)) + return c + +def difference(a, b): + if isinstance(a,collections.Hashable) and isinstance(b,collections.Hashable): + c = set(a) - set(b) + else: + c = unique(filter(lambda x: x not in b, a)) + return c + +def symmetric_difference(a, b): + if isinstance(a,collections.Hashable) and isinstance(b,collections.Hashable): + c = set(a) ^ set(b) + else: + c = unique(filter(lambda x: x not in intersect(a,b), union(a,b))) + return c + +def union(a, b): + if isinstance(a,collections.Hashable) and isinstance(b,collections.Hashable): + c = set(a) | set(b) + else: + c = unique(a + b) + return c + +def min(a): + _min = __builtins__.get('min') + return _min(a); + +def max(a): + _max = __builtins__.get('max') + return _max(a); + def isnotanumber(x): try: return math.isnan(x) @@ -61,9 +106,19 @@ class FilterModule(object): return { # general math 'isnan': isnotanumber, + 'min' : min, + 'max' : max, # exponents and logarithms 'log': logarithm, 'pow': power, 'root': inversepower, + + # set theory + 'unique' : unique, + 'intersect': intersect, + 'difference': difference, + 'symmetric_difference': symmetric_difference, + 'union': union, + } diff --git a/test/units/TestFilters.py b/test/units/TestFilters.py index d15147b0982..3c7eb4506e5 100644 --- a/test/units/TestFilters.py +++ b/test/units/TestFilters.py @@ -6,6 +6,7 @@ import os.path import unittest, tempfile, shutil from ansible import playbook, inventory, callbacks import ansible.runner.filter_plugins.core +import ansible.runner.filter_plugins.mathstuff INVENTORY = inventory.Inventory(['localhost']) @@ -182,9 +183,9 @@ class TestFilters(unittest.TestCase): self.assertTrue(ansible.runner.filter_plugins.core.version_compare('12.04', 12, 'ge')) def test_min(self): - a = ansible.runner.filter_plugins.core.min([3, 2, 5, 4]) + a = ansible.runner.filter_plugins.mathstuff.min([3, 2, 5, 4]) assert a == 2 def test_max(self): - a = ansible.runner.filter_plugins.core.max([3, 2, 5, 4]) + a = ansible.runner.filter_plugins.mathstuff.max([3, 2, 5, 4]) assert a == 5