Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/utils/io_utils.py: 49%
37 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
1# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
16"""Utilities related to disk I/O."""
18import os
19import sys
20import threading
22from absl import logging
24from keras.src.utils import keras_logging
26# isort: off
27from tensorflow.python.util.tf_export import keras_export
29INTERACTIVE_LOGGING = threading.local()
30INTERACTIVE_LOGGING.enable = keras_logging.INTERACTIVE_LOGGING_DEFAULT
33@keras_export("keras.utils.enable_interactive_logging")
34def enable_interactive_logging():
35 """Turn on interactive logging.
37 When interactive logging is enabled, Keras displays logs via stdout.
38 This provides the best experience when using Keras in an interactive
39 environment such as a shell or a notebook.
40 """
41 INTERACTIVE_LOGGING.enable = True
44@keras_export("keras.utils.disable_interactive_logging")
45def disable_interactive_logging():
46 """Turn off interactive logging.
48 When interactive logging is disabled, Keras sends logs to `absl.logging`.
49 This is the best option when using Keras in a non-interactive
50 way, such as running a training or inference job on a server.
51 """
52 INTERACTIVE_LOGGING.enable = False
55@keras_export("keras.utils.is_interactive_logging_enabled")
56def is_interactive_logging_enabled():
57 """Check if interactive logging is enabled.
59 To switch between writing logs to stdout and `absl.logging`, you may use
60 `keras.utils.enable_interactive_logging()` and
61 `keras.utils.disable_interactie_logging()`.
63 Returns:
64 Boolean (True if interactive logging is enabled and False otherwise).
65 """
66 # Use `getattr` in case `INTERACTIVE_LOGGING`
67 # does not have the `enable` attribute.
68 return getattr(
69 INTERACTIVE_LOGGING, "enable", keras_logging.INTERACTIVE_LOGGING_DEFAULT
70 )
73def print_msg(message, line_break=True):
74 """Print the message to absl logging or stdout."""
75 if is_interactive_logging_enabled():
76 if line_break:
77 sys.stdout.write(message + "\n")
78 else:
79 sys.stdout.write(message)
80 sys.stdout.flush()
81 else:
82 logging.info(message)
85def path_to_string(path):
86 """Convert `PathLike` objects to their string representation.
88 If given a non-string typed path object, converts it to its string
89 representation.
91 If the object passed to `path` is not among the above, then it is
92 returned unchanged. This allows e.g. passthrough of file objects
93 through this function.
95 Args:
96 path: `PathLike` object that represents a path
98 Returns:
99 A string representation of the path argument, if Python support exists.
100 """
101 if isinstance(path, os.PathLike):
102 return os.fspath(path)
103 return path
106def ask_to_proceed_with_overwrite(filepath):
107 """Produces a prompt asking about overwriting a file.
109 Args:
110 filepath: the path to the file to be overwritten.
112 Returns:
113 True if we can proceed with overwrite, False otherwise.
114 """
115 overwrite = (
116 input(f"[WARNING] {filepath} already exists - overwrite? [y/n]")
117 .strip()
118 .lower()
119 )
120 while overwrite not in ("y", "n"):
121 overwrite = (
122 input('Enter "y" (overwrite) or "n" (cancel).').strip().lower()
123 )
124 if overwrite == "n":
125 return False
126 print_msg("[TIP] Next time specify overwrite=True!")
127 return True