Table Of Contents

Previous topic

5. The ReaderError exception

Next topic

7. Subclassing InputReader

This Page

6. Helper Functions

6.1. abs_file_path()

input_reader.abs_file_path(filename, env=False)

This function takes a filename and returns the absolute path.

The reason this was written is that os.path.abspath() can convert a relative path, os.path.expandvars() can expand a shell variable, and os.path.expanduser() understands ~, but none of these does all three. This function piggybacks the three of these to guaruntee any path will be returned absolutely.

Parameters:
  • filename (str) – The path of the file that you wish to have the absolute path of.
  • env (bool, optional) – Replace the base part of the path with $HOME, if that is where the path is. The default is False.
Return type:

str

6.2. file_safety_check()

input_reader.file_safety_check(filename)

Function to check a file and raise an IOError if it is not “safe.” “Safe” meaning that the file exists and it is readable.

Parameters:filename (str) – The file you wish to check the safety of.
Return type:None
Exception:IOError : Raised when a file is not safe

6.3. range_check()

input_reader.range_check(low, high, expand=False, asint=False)

range_check() will verify that that given range has a low lower than the high. If both numbers are integers, it will return a list of the expanded range unless expand is False, in which it will just return the high and low. If low or high is not an integers, it will return the low and high values as floats.

Parameters:
  • low (float, int) – The low value if the range to check.
  • high (float, int) – The high value if the range to check.
  • expand (bool, optional) – If True and both low or high are integers, then range_check() will return the range of integers between low and high, inclusive. Otherwise, range_check() just returns low and high.
  • asint – If expand is False, this will attempt to return the low and high as integers instead of floats.
Return type:

See the explanation of expand.

Exception:
  • ValueError: low > high.
  • ValueError: low or high cannot be converted to a float.