"""The util module contains a variety of utility functions."""
import configparser
import sys


def read_cfg_file(filename):
    """Read and parse the experiment configuration file with configparser."""

    config = configparser.ConfigParser(inline_comment_prefixes=("#", ";"))
    config.optionxform = str

    try:
        out = config.read(str(filename))

        if not out and filename is not None:
            exit(f"\nERROR: The file '{filename}' is empty or does not exist!\n")

    except configparser.MissingSectionHeaderError:
        exit(f"\nERROR: You are missing a section heading in {filename:s}\n")

    except configparser.ParsingError:
        exit(
            "\nERROR: Having trouble reading your parameter file, did you"
            " forget '=' signs?\n{:s}".format(sys.exc_info()[1])
        )

    return config


def normalize_path(working_dir, filename):
    """Normalize the path of a filename relative to a specific directory."""

    path = filename

    if not path.is_absolute():
        path = working_dir / path

    return path.resolve()


def header1(string):
    """Print a formatted heading."""
    print(("\n".join(["", "", string, "=" * len(string), ""])))


def header2(string):
    """Print a formatted subheading."""
    print(("\n".join(["", string, "-" * len(string), ""])))

"""The util module contains a variety of utility functions."""
import configparser
import sys


def listfloat(text):
    return [float(val) for val in text.strip("[]").split(",")]


def read_cfg_file(filename=None):
    """Read and parse the experiment configuration file with configparser."""

    config = configparser.ConfigParser(
        comment_prefixes="#", inline_comment_prefixes="#", converters=listfloat
    )
    config.optionxform = str

    try:
        result = config.read(str(filename))

        if not result and filename is not None:
            exit(f"\nERROR: The file '{filename}' is empty or does not exist!\n")

    except configparser.MissingSectionHeaderError:
        exit(f"\nERROR: You are missing a section heading in {filename:s}\n")

    except configparser.ParsingError:
        exit(
            "\nERROR: Having trouble reading your parameter file, did you"
            " forget '=' signs?\n{:s}".format(sys.exc_info()[1])
        )

    return config


def normalize_path(working_dir, filename):
    """Normalize the path of a filename relative to a specific directory."""

    path = filename

    if not path.is_absolute():
        path = working_dir / path

    return path.resolve()


def header1(string):
    """Print a formatted heading."""
    print(("\n".join(["", "", string, "=" * len(string), ""])))


def header2(string):
    """Print a formatted subheading."""
    print(("\n".join(["", string, "-" * len(string), ""])))

Update settings for reading config files
Update settings for reading config files

Update the definition of comments, now only allowing the use of "#"
for comments. Add a converter function to parse list of floats,
such as:

    list_of_floats = [1.0, 2.0, 3.0]

  """The util module contains a variety of utility functions."""
  import configparser
  import sys
  
  
+ def listfloat(text):
+     return [float(val) for val in text.strip("[]").split(",")]
+ 
+ 
- def read_cfg_file(filename):
+ def read_cfg_file(filename=None):
      """Read and parse the experiment configuration file with configparser."""
  
-     config = configparser.ConfigParser(inline_comment_prefixes=("#", ";"))
+     config = configparser.ConfigParser(
+         comment_prefixes="#", inline_comment_prefixes="#", converters=listfloat
+     )
      config.optionxform = str
  
      try:
-         out = config.read(str(filename))
+         result = config.read(str(filename))
  
-         if not out and filename is not None:
+         if not result and filename is not None:
              exit(f"\nERROR: The file '{filename}' is empty or does not exist!\n")
  
      except configparser.MissingSectionHeaderError:
          exit(f"\nERROR: You are missing a section heading in {filename:s}\n")
  
      except configparser.ParsingError:
          exit(
              "\nERROR: Having trouble reading your parameter file, did you"
              " forget '=' signs?\n{:s}".format(sys.exc_info()[1])
          )
  
      return config
  
  
  def normalize_path(working_dir, filename):
      """Normalize the path of a filename relative to a specific directory."""
  
      path = filename
  
      if not path.is_absolute():
          path = working_dir / path
  
      return path.resolve()
  
  
  def header1(string):
      """Print a formatted heading."""
      print(("\n".join(["", "", string, "=" * len(string), ""])))
  
  
  def header2(string):
      """Print a formatted subheading."""
      print(("\n".join(["", string, "-" * len(string), ""])))
  
Update settings for reading config files
## Code Before:
"""The util module contains a variety of utility functions."""
import configparser
import sys


def read_cfg_file(filename):
    """Read and parse the experiment configuration file with configparser."""

    config = configparser.ConfigParser(inline_comment_prefixes=("#", ";"))
    config.optionxform = str

    try:
        out = config.read(str(filename))

        if not out and filename is not None:
            exit(f"\nERROR: The file '{filename}' is empty or does not exist!\n")

    except configparser.MissingSectionHeaderError:
        exit(f"\nERROR: You are missing a section heading in {filename:s}\n")

    except configparser.ParsingError:
        exit(
            "\nERROR: Having trouble reading your parameter file, did you"
            " forget '=' signs?\n{:s}".format(sys.exc_info()[1])
        )

    return config


def normalize_path(working_dir, filename):
    """Normalize the path of a filename relative to a specific directory."""

    path = filename

    if not path.is_absolute():
        path = working_dir / path

    return path.resolve()


def header1(string):
    """Print a formatted heading."""
    print(("\n".join(["", "", string, "=" * len(string), ""])))


def header2(string):
    """Print a formatted subheading."""
    print(("\n".join(["", string, "-" * len(string), ""])))

## Instruction:
Update settings for reading config files
## Code After:
"""The util module contains a variety of utility functions."""
import configparser
import sys


def listfloat(text):
    return [float(val) for val in text.strip("[]").split(",")]


def read_cfg_file(filename=None):
    """Read and parse the experiment configuration file with configparser."""

    config = configparser.ConfigParser(
        comment_prefixes="#", inline_comment_prefixes="#", converters=listfloat
    )
    config.optionxform = str

    try:
        result = config.read(str(filename))

        if not result and filename is not None:
            exit(f"\nERROR: The file '{filename}' is empty or does not exist!\n")

    except configparser.MissingSectionHeaderError:
        exit(f"\nERROR: You are missing a section heading in {filename:s}\n")

    except configparser.ParsingError:
        exit(
            "\nERROR: Having trouble reading your parameter file, did you"
            " forget '=' signs?\n{:s}".format(sys.exc_info()[1])
        )

    return config


def normalize_path(working_dir, filename):
    """Normalize the path of a filename relative to a specific directory."""

    path = filename

    if not path.is_absolute():
        path = working_dir / path

    return path.resolve()


def header1(string):
    """Print a formatted heading."""
    print(("\n".join(["", "", string, "=" * len(string), ""])))


def header2(string):
    """Print a formatted subheading."""
    print(("\n".join(["", string, "-" * len(string), ""])))
