Merge branch 'bcoca-sets_v2' into devel

pull/8465/head
James Cammarata 10 years ago
commit 936a909b5e

@ -23,6 +23,7 @@ import types
import pipes import pipes
import glob import glob
import re import re
import collections
import operator as py_operator import operator as py_operator
from ansible import errors from ansible import errors
from ansible.utils import md5s from ansible.utils import md5s
@ -145,19 +146,42 @@ def regex_replace(value='', pattern='', replacement='', ignorecase=False):
return _re.sub(replacement, value) return _re.sub(replacement, value)
def unique(a): def unique(a):
return set(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): def intersect(a, b):
return set(a).intersection(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): def difference(a, b):
return set(a).difference(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): def symmetric_difference(a, b):
return set(a).symmetric_difference(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): def union(a, b):
return set(a).union(b) if isinstance(a,collections.Hashable) and isinstance(b,collections.Hashable):
c = set(a) | set(b)
else:
c = unique(a + b)
return c
def version_compare(value, version, operator='eq', strict=False): def version_compare(value, version, operator='eq', strict=False):
''' Perform a version comparison on a value ''' ''' Perform a version comparison on a value '''

Loading…
Cancel
Save