|
def | match (my_pattern, string) |
|
def | match (pattern, string) |
|
|
def | init (self, string) |
|
def | tostr (self) |
|
def | torepr (self) |
|
def | __init__ (self, string, parent_cls=None) |
|
def | __new__ (cls, string, parent_cls=None) |
|
def | get_root (self) |
|
def | children (self) |
|
def | init (self, items) |
|
def | torepr (self) |
|
def | __str__ (self) |
|
def | __repr__ (self) |
|
def | tofortran (self, tab="", isfix=None) |
|
def | restore_reader (self, reader) |
|
def | __lt__ (self, other) |
|
def | __le__ (self, other) |
|
def | __eq__ (self, other) |
|
def | __ge__ (self, other) |
|
def | __gt__ (self, other) |
|
def | __ne__ (self, other) |
|
| string |
|
| parent |
|
| items |
|
| subclasses |
|
STRINGBase matches an upper case version of the input string with
another a pattern (typically taken from pattern_tools.py) and
returns the string in upper case if there is a match.
Definition at line 1424 of file utils.py.
◆ match()
def fparser.two.utils.STRINGBase.match |
( |
|
my_pattern, |
|
|
|
string |
|
) |
| |
|
static |
Matches an input string with a specified pattern. Casts the string
to upper case before performing a match and, if there is a
match, returns the string in upper case.
The pattern can be a regular expression, a string, a list or a
tuple. If the input pattern is a regular expression or a
string, a direct equivalence is performed. If the input pattern is a
list or a tuple, then all of the contents of the list
or tuple are searched for a match (by recursing). The list or tuple may
contain regular expressions, strings, lists or tuples. This
functionality can be used to recurse down a tree of lists and
or tuples until regular expressions or strings are found (at
the leaves of the tree) on which to match. The patterns used
to match in fparser can be found in patterns_tools.py. These
make use of the pattern class, whose match method behaves like
a regular expression. For example:
from fparser.two import pattern_tools
pattern = pattern_tools.intrinsic_type_name
result = STRINGBase.match(pattern, "logical")
:param pattern: the pattern to match
:type pattern: `list`, `tuple`, `str` or an `re` expression
:param str string: the string to match with the pattern
:return: None if there is no match, or a tuple containing the \
matched string in upper case.
:rtype: `NoneType` or ( `str` )
Definition at line 1432 of file utils.py.
1432 def match(my_pattern, string):
1433 """Matches an input string with a specified pattern. Casts the string 1434 to upper case before performing a match and, if there is a 1435 match, returns the string in upper case. 1437 The pattern can be a regular expression, a string, a list or a 1438 tuple. If the input pattern is a regular expression or a 1439 string, a direct equivalence is performed. If the input pattern is a 1440 list or a tuple, then all of the contents of the list 1441 or tuple are searched for a match (by recursing). The list or tuple may 1442 contain regular expressions, strings, lists or tuples. This 1443 functionality can be used to recurse down a tree of lists and 1444 or tuples until regular expressions or strings are found (at 1445 the leaves of the tree) on which to match. The patterns used 1446 to match in fparser can be found in patterns_tools.py. These 1447 make use of the pattern class, whose match method behaves like 1448 a regular expression. For example: 1450 from fparser.two import pattern_tools 1451 pattern = pattern_tools.intrinsic_type_name 1452 result = STRINGBase.match(pattern, "logical") 1454 :param pattern: the pattern to match 1455 :type pattern: `list`, `tuple`, `str` or an `re` expression 1456 :param str string: the string to match with the pattern 1457 :return: None if there is no match, or a tuple containing the \ 1458 matched string in upper case. 1459 :rtype: `NoneType` or ( `str` ) 1464 if not isinstance(string, str):
1465 raise InternalError(
1466 f
"Supplied string should be of type str, but found {type(string)}" 1468 if isinstance(my_pattern, (list, tuple)):
1469 for child
in my_pattern:
1470 result = STRINGBase.match(child, string)
1474 string_upper = string.upper()
1475 if isinstance(my_pattern, str):
1476 if len(my_pattern) == len(string)
and my_pattern == string_upper:
1477 return (string_upper,)
1480 if my_pattern.match(string_upper):
1481 return (string_upper,)
1482 except AttributeError:
1483 raise InternalError(
1484 f
"Supplied pattern should be a list, tuple, str or regular " 1485 f
"expression but found {type(my_pattern)}"
The documentation for this class was generated from the following file:
- /home/docs/checkouts/readthedocs.org/user_builds/fparser-ref/checkouts/342_reference_guide/src/fparser/two/utils.py