CommentCollection#
- class montepy.CommentCollection(comments: Iterable[CommentNode] = ()) None#
Bases:
SequenceA read-only collection of the comments in an object that supports searching by text.
This is a
SequenceofCommentNodeinstances, so it supports indexing, slicing, iteration, andlen(). Checking a string with theinoperator searches the text of all comments, so an object can be found by its comments; anything other than a string keeps normal membership behavior.Added in version 1.5.0.
Examples
>>> import montepy >>> problem = montepy.read_input("foo.imcnp") >>> "light water" in problem.materials[1].comments True >>> import re >>> problem.materials[1].comments.search(re.compile(r"(?i)LIGHT"))[0].contents 'light water'
- Parameters:
comments (collections.abc.Iterable) – the comments in this collection, as an iterable of
CommentNode.
Methods:
count(value)index(value, [start, [stop]])Raises ValueError if the value is not present.
search(pattern)Searches the text of the comments in this collection.
- count(value) integer -- return number of occurrences of value#
- index(value[, start[, stop]]) integer -- return first index of value.#
Raises ValueError if the value is not present.
Supporting start and stop arguments is optional, but recommended.
- search(pattern: str | Pattern) CommentCollection#
Searches the text of the comments in this collection.
The search is run against each comment’s
contents, that is the comment’s text without its delimiters (c/$).Added in version 1.5.0.
- Parameters:
pattern (str or re.Pattern) – a substring to search for, or a compiled regular expression (e.g.
re.compile(pattern, re.IGNORECASE)).- Returns:
a new collection of the comments that matched.
- Return type:
- Raises:
TypeError – if
patternis not a str, or a pattern compiled from a str.