Commit ee4c0997 authored by Patrick Shriwise's avatar Patrick Shriwise
Browse files

Removing f-strings in the checkvalue module

parent e1280cdd
Loading
Loading
Loading
Loading
+52 −49
Original line number Diff line number Diff line
import copy
from collections.abc import Iterable
from multiprocessing.sharedctypes import Value
from collections import Iterable
from pathlib import Path

import numpy as np

# Provided from OpenMC (commit c8003e0)
# modified to remove f-strings for compatibility with Python 2

def check_type(name, value, expected_type, expected_iter_type=None, *, none_ok=False):
def check_type(name, value, expected_type, expected_iter_type=None, none_ok=False):
    """Ensure that an object is of an expected type. Optionally, if the object is
    iterable, check that each element is of a particular type.

@@ -35,16 +35,16 @@ def check_type(name, value, expected_type, expected_iter_type=None, *, none_ok=F
                  'following types: "{}"'.format(name, value, ', '.join(
                      [t.__name__ for t in expected_type]))
        else:
            msg = (f'Unable to set "{name}" to "{value}" which is not of type "'
                   f'{expected_type.__name__}"')
        raise TypeError(msg)
            msg = 'Unable to set "{}" to "{}" which is not of type "{}"'

        raise TypeError(msg.format(name, value, expected_type.__name__))

    if expected_iter_type:
        if isinstance(value, np.ndarray):
            if not issubclass(value.dtype.type, expected_iter_type):
                msg = (f'Unable to set "{name}" to "{value}" since each item '
                       f'must be of type "{expected_iter_type.__name__}"')
                raise TypeError(msg)
                msg = ('Unable to set "{}" to "{}" since each item '
                       'must be of type "{}"')
                raise TypeError(msg.format(name, value, expected_type.__name__))
            else:
                return

@@ -56,9 +56,9 @@ def check_type(name, value, expected_type, expected_iter_type=None, *, none_ok=F
                              name, value, ', '.join([t.__name__ for t in
                                                      expected_iter_type]))
                else:
                    msg = (f'Unable to set "{name}" to "{value}" since each '
                           f'item must be of type "{expected_iter_type.__name__}"')
                raise TypeError(msg)
                    msg = ('Unable to set "{name}" to "{value}" since each '
                           'item must be of type "{}"')
                raise TypeError(msg.format(name, value, expected_iter_type.__name__))


def check_file(name, value, must_exist=False):
@@ -131,9 +131,9 @@ def check_iterable_type(name, value, expected_type, min_depth=1, max_depth=1):
        if isinstance(current_item, expected_type):
            # Is this deep enough?
            if len(tree) < min_depth:
                msg = (f'Error setting "{name}": The item at {ind_str} does not '
                       f'meet the minimum depth of {min_depth}')
                raise TypeError(msg)
                msg = ('Error setting "{}": The item at {} does not '
                       'meet the minimum depth of {}')
                raise TypeError(msg.format(name, ind_str, min_depth))

            # This item is okay.  Move on to the next item.
            index[-1] += 1
@@ -148,17 +148,19 @@ def check_iterable_type(name, value, expected_type, min_depth=1, max_depth=1):

                # But first, have we exceeded the max depth?
                if len(tree) > max_depth:
                    msg = (f'Error setting {name}: Found an iterable at '
                           f'{ind_str}, items in that iterable exceed the '
                           f'maximum depth of {max_depth}')
                    raise TypeError(msg)
                    msg = ('Error setting {}: Found an iterable at '
                           '{}, items in that iterable exceed the '
                           'maximum depth of {}')
                    raise TypeError(msg.format(name, ind_str, max_depth))

            else:
                # This item is completely unexpected.
                msg = (f'Error setting {name}: Items must be of type '
                       f'"{expected_type.__name__}", but item at {ind_str} is '
                       f'of type "{type(current_item).__name__}"')
                raise TypeError(msg)
                msg = ('Error setting {}: Items must be of type '
                       '"{}", but item at {} is '
                       'of type "{}"')
                raise TypeError(msg.format(name,
                                           expected_type.__name__,
                                           type(current_item).__name__))


def check_length(name, value, length_min, length_max=None):
@@ -180,17 +182,17 @@ def check_length(name, value, length_min, length_max=None):

    if length_max is None:
        if len(value) < length_min:
            msg = (f'Unable to set "{name}" to "{value}" since it must be at '
                   f'least of length "{length_min}"')
            raise ValueError(msg)
            msg = ('Unable to set "{}" to "{}" since it must be at '
                   'least of length "{}"')
            raise ValueError(msg.format(name, value, length_min))
    elif not length_min <= len(value) <= length_max:
        if length_min == length_max:
            msg = (f'Unable to set "{name}" to "{value}" since it must be of '
                  f'length "{length_min}"')
            msg = ('Unable to set "{name}" to "{value}" since it must be of '
                   'length "{length_min}"')
        else:
            msg = (f'Unable to set "{name}" to "{value}" since it must have '
                   f'length between "{length_min}" and "{length_max}"')
        raise ValueError(msg)
            msg = ('Unable to set "{}" to "{}" since it must have '
                   'length between "{}" and "{}"')
        raise ValueError(msg.format(name, value, length_min, length_max))


def check_value(name, value, accepted_values):
@@ -208,9 +210,9 @@ def check_value(name, value, accepted_values):
    """

    if value not in accepted_values:
        msg = (f'Unable to set "{name}" to "{value}" since it is not in '
               f'"{accepted_values}"')
        raise ValueError(msg)
        msg = ('Unable to set "{}" to "{}" since it is not in '
               '"{}"')
        raise ValueError(msg.format(name, value, accepted_values))


def check_less_than(name, value, maximum, equality=False):
@@ -231,14 +233,14 @@ def check_less_than(name, value, maximum, equality=False):

    if equality:
        if value > maximum:
            msg = (f'Unable to set "{name}" to "{value}" since it is greater '
                   f'than "{maximum}"')
            raise ValueError(msg)
            msg = ('Unable to set "{}" to "{}" since it is greater '
                   'than "{}"')
            raise ValueError(msg.format(name, value, maximum))
    else:
        if value >= maximum:
            msg = (f'Unable to set "{name}" to "{value}" since it is greater '
                   f'than or equal to "{maximum}"')
            raise ValueError(msg)
            msg = ('Unable to set "{}" to "{}" since it is greater '
                   'than or equal to "{}"')
            raise ValueError(msg.format(name, value, maximum))


def check_greater_than(name, value, minimum, equality=False):
@@ -259,14 +261,14 @@ def check_greater_than(name, value, minimum, equality=False):

    if equality:
        if value < minimum:
            msg = (f'Unable to set "{name}" to "{value}" since it is less than '
                   f'"{minimum}"')
            raise ValueError(msg)
            msg = ('Unable to set "{}" to "{}" since it is less than '
                   '"{}"')
            raise ValueError(msg.format(name, value, minimum))
    else:
        if value <= minimum:
            msg = (f'Unable to set "{name}" to "{value}" since it is less than '
                   f'or equal to "{minimum}"')
            raise ValueError(msg)
            msg = ('Unable to set "{}" to "{}" since it is less than '
                   'or equal to "{}"')
            raise ValueError(msg.formaat(name, value, minimum))


def check_filetype_version(obj, expected_type, expected_version):
@@ -288,7 +290,8 @@ def check_filetype_version(obj, expected_type, expected_version):

        # Check filetype
        if this_filetype != expected_type:
            raise IOError(f'{obj.filename} is not a {expected_type} file.')
            msg = '{} is not a {} file.'
            raise IOError(msg.format(obj.filename, expected_type))

        # Check version
        if this_version[0] != expected_version:
@@ -298,9 +301,9 @@ def check_filetype_version(obj, expected_type, expected_version):
                                  '.'.join(str(v) for v in this_version),
                                  expected_version))
    except AttributeError:
        raise IOError(f'Could not read {obj.filename} file. This most likely '
        raise IOError('Could not read {} file. This most likely '
                      'means the file was produced by a different version of '
                      'OpenMC than the one you are using.')
                      'OpenMC than the one you are using.'.format(obj.filename))


class CheckedList(list):