CommentCollection#

class montepy.CommentCollection(comments: Iterable[CommentNode] = ()) None#

Bases: Sequence

A read-only collection of the comments in an object that supports searching by text.

This is a Sequence of CommentNode instances, so it supports indexing, slicing, iteration, and len(). Checking a string with the in operator 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:

CommentCollection

Raises:

TypeError – if pattern is not a str, or a pattern compiled from a str.