( strs, caseless=False, useRegex=True )
return MatchFirst(parseElementClass(sym) for sym in symbols).setName(' | '.join(symbols))
Helper to quickly define a set of alternative Literals, and makes sure to do
    longest-first testing when there is a conflict, regardless of the input order,
    but returns a C{L{MatchFirst}} for best performance.

    Parameters:
     - strs - a string of space-delimited literals, or a collection of string literals
     - caseless - (default=C{False}) - treat all literals as caseless
     - useRegex - (default=C{True}) - as an optimization, will generate a Regex
          object; otherwise, will generate a C{MatchFirst} object (if C{caseless=True}, or
          if creating a C{Regex} raises an exception)

    Example::
        comp_oper = oneOf("< = > <= >= !=")
        var = Word(alphas)
        number = Word(nums)
        term = var | number
        comparison_expr = term + comp_oper + term
        print(comparison_expr.searchString("B = 12  AA=23 B<=AA AA>12"))
    prints::
        [['B', '=', '12'], ['AA', '=', '23'], ['B', '<=', 'AA'], ['AA', '>', '12']]
Helper to quickly define a set of alternative Literals, and makes sure to do
    longest-first testing when there is a conflict, regardless of the input order,
    but returns a C{L{MatchFirst}} for best performance.
Helper to quickly define a set of alternative Literals and makes sure to do longest - first testing when there is a conflict regardless of the input order but returns a C { L { MatchFirst }} for best performance .
def oneOf( strs, caseless=False, useRegex=True ):
    """
    Helper to quickly define a set of alternative Literals, and makes sure to do
    longest-first testing when there is a conflict, regardless of the input order,
    but returns a C{L{MatchFirst}} for best performance.

    Parameters:
     - strs - a string of space-delimited literals, or a collection of string literals
     - caseless - (default=C{False}) - treat all literals as caseless
     - useRegex - (default=C{True}) - as an optimization, will generate a Regex
          object; otherwise, will generate a C{MatchFirst} object (if C{caseless=True}, or
          if creating a C{Regex} raises an exception)

    Example::
        comp_oper = oneOf("< = > <= >= !=")
        var = Word(alphas)
        number = Word(nums)
        term = var | number
        comparison_expr = term + comp_oper + term
        print(comparison_expr.searchString("B = 12  AA=23 B<=AA AA>12"))
    prints::
        [['B', '=', '12'], ['AA', '=', '23'], ['B', '<=', 'AA'], ['AA', '>', '12']]
    """
    if caseless:
        isequal = ( lambda a,b: a.upper() == b.upper() )
        masks = ( lambda a,b: b.upper().startswith(a.upper()) )
        parseElementClass = CaselessLiteral
    else:
        isequal = ( lambda a,b: a == b )
        masks = ( lambda a,b: b.startswith(a) )
        parseElementClass = Literal

    symbols = []
    if isinstance(strs,basestring):
        symbols = strs.split()
    elif isinstance(strs, Iterable):
        symbols = list(strs)
    else:
        warnings.warn("Invalid argument to oneOf, expected string or iterable",
                SyntaxWarning, stacklevel=2)
    if not symbols:
        return NoMatch()

    i = 0
    while i < len(symbols)-1:
        cur = symbols[i]
        for j,other in enumerate(symbols[i+1:]):
            if ( isequal(other, cur) ):
                del symbols[i+j+1]
                break
            elif ( masks(cur, other) ):
                del symbols[i+j+1]
                symbols.insert(i,other)
                cur = other
                break
        else:
            i += 1

    if not caseless and useRegex:
        #~ print (strs,"->", "|".join( [ _escapeRegexChars(sym) for sym in symbols] ))
        try:
            if len(symbols)==len("".join(symbols)):
                return Regex( "[%s]" % "".join(_escapeRegexRangeChars(sym) for sym in symbols) ).setName(' | '.join(symbols))
            else:
                return Regex( "|".join(re.escape(sym) for sym in symbols) ).setName(' | '.join(symbols))
        except Exception:
            warnings.warn("Exception creating Regex for oneOf, building MatchFirst",
                    SyntaxWarning, stacklevel=2)


    # last resort, just use MatchFirst
    return MatchFirst(parseElementClass(sym) for sym in symbols).setName(' | '.join(symbols))
def oneOf ( strs , caseless = False , useRegex = True ) : if caseless : isequal = ( lambda a , b : a . upper ( ) == b . upper ( ) ) masks = ( lambda a , b : b . upper ( ) . startswith ( a . upper ( ) ) ) parseElementClass = CaselessLiteral else : isequal = ( lambda a , b : a == b ) masks = ( lambda a , b : b . startswith ( a ) ) parseElementClass = Literal symbols = [ ] if isinstance ( strs , basestring ) : symbols = strs . split ( ) elif isinstance ( strs , Iterable ) : symbols = list ( strs ) else : warnings . warn ( "Invalid argument to oneOf, expected string or iterable" , SyntaxWarning , stacklevel = 2 ) if not symbols : return NoMatch ( ) i = 0 while i < len ( symbols ) - 1 : cur = symbols [ i ] for j , other in enumerate ( symbols [ i + 1 : ] ) : if ( isequal ( other , cur ) ) : del symbols [ i + j + 1 ] break elif ( masks ( cur , other ) ) : del symbols [ i + j + 1 ] symbols . insert ( i , other ) cur = other break else : i += 1 if not caseless and useRegex : #~ print (strs,"->", "|".join( [ _escapeRegexChars(sym) for sym in symbols] )) try : if len ( symbols ) == len ( "" . join ( symbols ) ) : return Regex ( "[%s]" % "" . join ( _escapeRegexRangeChars ( sym ) for sym in symbols ) ) . setName ( ' | ' . join ( symbols ) ) else : return Regex ( "|" . join ( re . escape ( sym ) for sym in symbols ) ) . setName ( ' | ' . join ( symbols ) ) except Exception : warnings . warn ( "Exception creating Regex for oneOf, building MatchFirst" , SyntaxWarning , stacklevel = 2 ) # last resort, just use MatchFirst return MatchFirst ( parseElementClass ( sym ) for sym in symbols ) . setName ( ' | ' . join ( symbols ) )