Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add longer descriptions to impls in flags #1595

Merged
merged 4 commits into from
Jan 24, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 54 additions & 57 deletions armi/reactor/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,58 +136,6 @@ def __fromStringGeneral(cls, typeSpec, updateMethod):
return result


def _fromStringIgnoreErrors(cls, typeSpec):
"""
Convert string into a set of flags.

Each word can be its own flag.

Notes
-----
This ignores words in the typeSpec that are not valid flags.

Complications arise when:

a. multiple-word flags are used such as *grid plate* or
*inlet nozzle* so we use lookups.
b. Some flags have digits in them. We just strip those off.
"""

def updateMethodIgnoreErrors(typeSpec):
try:
return cls[typeSpec]
except KeyError:
return cls(0)

return __fromStringGeneral(cls, typeSpec, updateMethodIgnoreErrors)


def _fromString(cls, typeSpec):
"""Make flag from string and fail if any unknown words are encountered."""

def updateMethod(typeSpec):
try:
return cls[typeSpec]
except KeyError:
raise InvalidFlagsError(
"The requested type specification `{}` is invalid. "
"See armi.reactor.flags documentation.".format(typeSpec)
)

return __fromStringGeneral(cls, typeSpec, updateMethod)


def _toString(cls, typeSpec):
"""
Make flag from string and fail if any unknown words are encountered.

Notes
-----
This converts a flag from ``Flags.A|B`` to ``'A B'``
"""
return str(typeSpec).split("{}.".format(cls.__name__))[1].replace("|", " ")


class Flags(Flag):
"""Defines the valid flags used in the framework."""

Expand Down Expand Up @@ -280,29 +228,78 @@ class Flags(Flag):

@classmethod
def fromStringIgnoreErrors(cls, typeSpec):
return _fromStringIgnoreErrors(cls, typeSpec)
"""
Convert string into a set of flags.

Each word can be its own flag.

Notes
-----
This ignores words in the typeSpec that are not valid flags.

Complications arise when:

a. multiple-word flags are used such as *grid plate* or
*inlet nozzle* so we use lookups.
b. Some flags have digits in them. We just strip those off.
"""

def updateMethodIgnoreErrors(typeSpec):
try:
return cls[typeSpec]
except KeyError:
return cls(0)

return __fromStringGeneral(cls, typeSpec, updateMethodIgnoreErrors)

@classmethod
def fromString(cls, typeSpec):
"""
Retrieve flag from a string.
Retrieve flag from a string and fail if any unknown words are encountered.

.. impl:: Retrieve flag from a string.
:id: I_ARMI_FLAG_TO_STR0
:implements: R_ARMI_FLAG_TO_STR

For a string passed as ``typeSpec``, first converts the whole string
to uppercase.
Then tries to parse the string for any special phrases, as defined
in the module dictionary ``_CONVERSIONS``, and converts those phrases
to flags directly.
john-science marked this conversation as resolved.
Show resolved Hide resolved

Then splits the remaining string into separate words based on the presence
john-science marked this conversation as resolved.
Show resolved Hide resolved
of spaces. Looping over each of the words, any numbers are stripped out
and the remaining string is matched up to any class attribute names.
If any matches are found these are returned as flags.
"""
return _fromString(cls, typeSpec)

def updateMethod(typeSpec):
try:
return cls[typeSpec]
except KeyError:
raise InvalidFlagsError(
"The requested type specification `{}` is invalid. "
"See armi.reactor.flags documentation.".format(typeSpec)
)

return __fromStringGeneral(cls, typeSpec, updateMethod)

@classmethod
def toString(cls, typeSpec):
"""
Convert a flag to a string.
Make flag from string and fail if any unknown words are encountered.

.. impl:: Convert a flag to string.
:id: I_ARMI_FLAG_TO_STR1
:implements: R_ARMI_FLAG_TO_STR

This converts the representation of a bunch of flags from ``typeSpec``,
which might look like ``Flags.A|B``,
into a string with spaces in between the flag names, which would look
like ``'A B'``. This is done via nesting string splitting and replacement
actions.
"""
return _toString(cls, typeSpec)
return str(typeSpec).split("{}.".format(cls.__name__))[1].replace("|", " ")
keckler marked this conversation as resolved.
Show resolved Hide resolved


class InvalidFlagsError(KeyError):
Expand Down
Loading