montepy.utilities module#

Functions:

args_checked(func)

A function decorator that enforces type annotations for the function when ran.

fortran_float(number_string)

Attempts to convert a FORTRAN formatted float string to a float.

is_comment(line)

Determines if the line is a C comment style comment.

make_prop_pointer(hidden_param[, types, ...])

A decorator function that makes a property based off of a pointer to another object.

make_prop_val_node(hidden_param[, types, ...])

A decorator function for making a property from a ValueNode.

montepy.utilities.args_checked(func: Callable)#

A function decorator that enforces type annotations for the function when ran.

This will read the type annotations for all arguments and enforce that the argument is of that type at run-time. Value restrictions can also be applied as well with typing.Annotated.

Examples

This will ensure that all arguments have to be an integer,

>>> from montepy.utilities import *
>>> import montepy.types as ty
>>>
>>> @args_checked
... def foo(a: int) -> int:
...    return a
>>> print(foo(1))
1
>>> print(foo("a"))
Traceback (most recent call last):
...
TypeError: Unable to set "a" for "foo" to "a" which is not of type "int"

Values can be checked as by using typing.Annotated to add an annotation of the values that are allowed (via a special value checking function),

>>> import typing
>>> import montepy.types as ty
>>>
>>> @args_checked
... def bar(a: typing.Annotated[int, ty.positive]) -> int:
...     return a
>>> print(bar(1))
1
>>> print(bar(0))
Traceback (most recent call last):
...
ValueError: Unable to set "a" for "bar" to "0" since it is less than or equal to "0"
Parameters:

func (collections.abc.Callable) – The function to decorate

Return type:

A decorated function that will do type and value checking at run time based on the annotation.

Raises:
  • TypeError – If an argument of the wrong type is provided

  • ValueError – If an argument of the right type, but wrong value is given.

montepy.utilities.fortran_float(number_string: str | Real)#

Attempts to convert a FORTRAN formatted float string to a float.

FORTRAN allows silly things for scientific notation like 6.02+23 to represent Avogadro’s Number.

Parameters:

number_string (str) – the string that will be converted to a float

Raises:

ValueError – If the string can not be parsed as a float.

Returns:

the parsed float of the this string

Return type:

float

montepy.utilities.is_comment(line: str)#

Determines if the line is a C comment style comment.

Parameters:

line (str) – the line to analyze

Returns:

True if the line is a comment

Return type:

bool

montepy.utilities.make_prop_pointer(hidden_param, types=None, base_type=None, validator=None, deletable=False)#

A decorator function that makes a property based off of a pointer to another object.

Note this can also be used for almost any circumstance as everything in python is a pointer.

Parameters:
  • hidden_param (str) – The string representing the parameter name of the internally stored ValueNode.

  • types (Class, tuple) – the acceptable types for the settable, which is passed to isinstance, if an empty tuple is provided the type will be self.

  • validator (function) – A validator function to run on values before setting. Must accept func(self, value).

  • deletable (bool) – If true make this property deletable. When deleted the value will be set to None.

montepy.utilities.make_prop_val_node(hidden_param, types=None, base_type=None, validator=None, deletable=False)#

A decorator function for making a property from a ValueNode.

This decorator is meant to handle all boiler plate. It will get and set the value property of the underlying ValueNode. By default the property is not settable unless types is set.

Parameters:
  • hidden_param (str) – The string representing the parameter name of the internally stored ValueNode.

  • types (Class, tuple) – the acceptable types for the settable, which is passed to isinstance. If an empty tuple will be type(self).

  • validator (function) – A validator function to run on values before setting. Must accept func(self, value).

  • deletable (bool) – If true make this property deletable. When deleted the value will be set to None.