Revision a2d2e1a7 lib/utils.py
b/lib/utils.py | ||
---|---|---|
1818 | 1818 |
# This is not nice and not absolutely atomic, but it appears to be the only |
1819 | 1819 |
# solution in Python -- there are no atomic types. |
1820 | 1820 |
self.called = True |
1821 |
|
|
1822 |
|
|
1823 |
class FieldSet(object): |
|
1824 |
"""A simple field set. |
|
1825 |
|
|
1826 |
Among the features are: |
|
1827 |
- checking if a string is among a list of static string or regex objects |
|
1828 |
- checking if a whole list of string matches |
|
1829 |
- returning the matching groups from a regex match |
|
1830 |
|
|
1831 |
Internally, all fields are held as regular expression objects. |
|
1832 |
|
|
1833 |
""" |
|
1834 |
def __init__(self, *items): |
|
1835 |
self.items = [re.compile("^%s$" % value) for value in items] |
|
1836 |
|
|
1837 |
def Extend(self, other_set): |
|
1838 |
"""Extend the field set with the items from another one""" |
|
1839 |
self.items.extend(other_set.items) |
|
1840 |
|
|
1841 |
def Matches(self, field): |
|
1842 |
"""Checks if a field matches the current set |
|
1843 |
|
|
1844 |
@type field: str |
|
1845 |
@param field: the string to match |
|
1846 |
@return: either False or a regular expression match object |
|
1847 |
|
|
1848 |
""" |
|
1849 |
for m in itertools.ifilter(None, (val.match(field) for val in self.items)): |
|
1850 |
return m |
|
1851 |
return False |
|
1852 |
|
|
1853 |
def NonMatching(self, items): |
|
1854 |
"""Returns the list of fields not matching the current set |
|
1855 |
|
|
1856 |
@type items: list |
|
1857 |
@param items: the list of fields to check |
|
1858 |
@rtype: list |
|
1859 |
@return: list of non-matching fields |
|
1860 |
|
|
1861 |
""" |
|
1862 |
return [val for val in items if not self.Matches(val)] |
Also available in: Unified diff