pytilities.overloading.matchers

Package pytilities :: Package overloading :: Module matchers |
[hide private]
[frames] | no frames]

# Source Code for Module pytilities.overloading.matchers

     1  # Copyright (C) 2010 Tim Diels <limyreth@users.sourceforge.net>
     2  #
     3  # This file is part of pytilities.
     4  #
     5  # pytilities 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  # pytilities 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 pytilities.  If not, see <http://www.gnu.org/licenses/>.
    17  #
    18
    19  __docformat__ = 'reStructuredText'
    20
    21 -def matches_everything(value):
    22      """A matcher that always returns true
    23      """
    24      return True
    25
    26 -def matches_type(*types):
    27      """A matcher that matches values which are instances of any of given types
    28      types: -- collection of types
    29      """
    30      def matcher(value):
    31          for type_ in types:
    32              if isinstance(value, type_):
    33                  return True
    34
    35          return False
    36
    37      return matcher
    38
    39 -def matches_all(*matchers):
    40      """A matcher that matches values which match all given matchers
    41      matchers: -- collection of matcher functions
    42      """
    43      def matcher(value):
    44          for matcher in matchers:
    45              if not matcher(value):
    46                  return False
    47
    48          return True
    49
    50      return matcher
    51
    52 -def matches_any(*matchers):
    53      """A matcher that matches values which are instances of any of given types
    54      matchers: -- ordered sequence of matcher functions
    55      """
    56      def matcher(value):
    57          for matcher in matchers:
    58              if matcher(value):
    59                  return True
    60
    61          return False
    62
    63      return matcher
    64
    65 -def matches_none(*matchers):
    66      """A matcher that matches values which are instances of any of given types
    67      matchers: -- collection of matcher functions
    68      """
    69      def matcher(value):
    70          for matcher in matchers:
    71              if matcher(value):
    72                  return False
    73
    74          return True
    75
    76      return matcher
    77