rads.utility module¶
Utility functions.
-
rads.utility.ensure_open(file: Union[str, os.PathLike, IO[Any]], mode: str = 'r', buffering: int = -1, encoding: Optional[str] = None, errors: Optional[str] = None, newline: Optional[str] = None, closefd: bool = True, closeio: bool = False) → IO[Any][source]¶ Open file or leave file-like object open.
This function behaves identically to
open()but can also accept a file-like object in the file parameter.- Parameters
file –
A path-like object giving the pathname (absolute or relative to the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped or a file-like object.
Note
If a file descriptor is given, it is closed when the returned I/O object is closed, unless closefd is set to False.
Note
If a file-like object is given closing the returned I/O object will not close the given file unless closeio is set to True.
mode – See
open()buffering – See
open()encoding – See
open()errors – See
open()newline – See
open()closefd – See
open()closeio – If set to True then if file is a file like object it will be closed when either the __exit__ or close methods are called on the returned I/O object. By default these methods will be ignored when file is a file-like object.
- Returns
An I/O object or the original file-like object if file is a file-like object. If this is the original file-like object and closeio is set to False (the default) then it’s close and __exit__ methods will be no-ops.
See also
-
rads.utility.filestring(file: Union[str, os.PathLike, IO[Any]]) → Optional[str][source]¶ Convert a PathLikeOrFile to a string.
- Parameters
file – file or file-like object to get the string for.
- Returns
The string representation of the filename or path. If it cannot get the name/path of the given file or file-like object or cannot convert it to a str, None will be returned.
-
rads.utility.isio(obj: Any, *, read: bool = False, write: bool = False) → bool[source]¶ Determine if object is IO like and is read and/or write.
Note
Falls back to
isinstnace(obj, io.IOBase)if neither read nor write is True.- Parameters
obj – Object to check if it is an IO like object.
read – Require obj to be readable if True.
write – Require obj to be writable if True.
- Returns
True if the given obj is readable and/or writeable as defined by the read and write arguments.
-
rads.utility.xor(a: bool, b: bool) → bool[source]¶ Boolean XOR operator.
This implements the XOR boolean operator and has the following truth table:
a
b
a XOR b
True
True
False
True
False
True
False
True
True
False
False
False
- Parameters
a – First boolean value.
b – Second boolean value.
- Returns
The result of a XOR b from the truth table above.
-
rads.utility.contains_sublist(list_: List[Any], sublist: List[Any]) → bool[source]¶ Determine if a list contains a sublist.
- Parameters
list – list to search for the sublist in.
sublist – Sub list to search for.
- Returns
True if list contains sublist.
-
rads.utility.merge_sublist(list_: List[Any], sublist: List[Any]) → List[Any][source]¶ Merge a sublist into a given list_.
- Parameters
list – List to merge sublist into.
sublist – Sublist to merge into list_
- Returns
A copy of list_ with sublist at the end if sublist is not a sublist of list_. Otherwise, a copy of list_ is returned unchanged.
-
rads.utility.delete_sublist(list_: List[Any], sublist: List[Any]) → List[Any][source]¶ Remove a sublist from the given list_.
- Parameters
list – List to remove the sublist from.
sublist – Sublist to remove from list_.
- Returns
A copy of list_ with the sublist removed.
-
rads.utility.fortran_float(string: str) → float[source]¶ Construct
floatfrom Fortran style float strings.This function can convert strings to floats in all of the formats below:
Note
Because RADS was written in Fortran, exponent characters in configuration and passindex files sometimes use ‘D’ or ‘d’ as the exponent separator instead of ‘E’ or ‘e’.
Warning
If you are Fortran developer stop using ‘Ew.d’ and ‘Ew.dDe’ formats and use ‘Ew.dEe’ instead. The first two are not commonly supported by other languages while the last version is the standard for nearly all languages. Ok, rant over.
- Parameters
string – String to attempt to convert to a float.
- Returns
The float parsed from the given string.
- Raises
ValueError – If string does not represent a valid float.
-
rads.utility.datetime_to_timestamp(time: datetime.datetime, *, epoch: datetime.datetime = datetime.datetime(1985, 1, 1, 0, 0)) → float[source]¶ Convert datetime object to timestamp relative to an epoch.
- Parameters
time – Date and time.
epoch – Date and time of epoch. Defaults to the RADS epoch.
- Returns
The number of seconds between the epoch and the given time.
-
rads.utility.timestamp_to_datetime(seconds: float, *, epoch: datetime.datetime = datetime.datetime(1985, 1, 1, 0, 0)) → datetime.datetime[source]¶ Convert timestamp relative to an epoch to a datetime.
- Parameters
seconds – Seconds since the given epoch.
epoch – Date and time of epoch. Defaults to the RADS epoch.
- Returns
Date and time corresponding to the given seconds since the epoch.