Skip to content

Commit

Permalink
rearranged math filters
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoca committed Feb 19, 2015
1 parent 94aca71 commit 8872bba
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 58 deletions.
54 changes: 0 additions & 54 deletions lib/ansible/runner/filter_plugins/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import pipes
import glob
import re
import collections
import crypt
import hashlib
import string
Expand Down Expand Up @@ -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 '''
Expand Down Expand Up @@ -386,14 +340,6 @@ def filters(self):
'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,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,56 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.

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)
Expand Down Expand Up @@ -61,9 +106,19 @@ def filters(self):
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,

}
5 changes: 3 additions & 2 deletions test/units/TestFilters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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'])

Expand Down Expand Up @@ -182,9 +183,9 @@ def test_version_compare(self):
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

0 comments on commit 8872bba

Please sign in to comment.