Skip to content

Commit f8b0630

Browse files
authored
Use logging instead of print statements (#18)
1 parent af53a96 commit f8b0630

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

cache_magic/__init__.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import shutil
99
import ast
1010
import astunparse
11+
import logging
1112
from tabulate import tabulate
1213
from IPython.display import HTML, display
1314

@@ -52,16 +53,16 @@ def __call__(self, version="*", reset=False, var_name="", var_value="", show_all
5253

5354
if reset:
5455
if var_name:
55-
print("resetting cached values for " + var_name)
56+
logging.warning("resetting cached values for " + var_name)
5657
self._reset_var(var_folder_path)
5758
# no return, because it might be a forced recalculation
5859
else:
59-
print("resetting entire cache")
60+
logging.warning("resetting entire cache")
6061
self._reset_all(base_dir)
6162
return
6263

6364
if not var_name:
64-
print("Warning: nothing todo: no variable defined, no reset requested, no show_all requested. ")
65+
logging.warning("Warning: nothing todo: no variable defined, no reset requested, no show_all requested. ")
6566
return
6667

6768
version = self._get_cache_version(version, var_value, user_ns)
@@ -74,7 +75,7 @@ def __call__(self, version="*", reset=False, var_name="", var_value="", show_all
7475
try:
7576
stored_value = self.get_from_file(var_data_path)
7677

77-
print('loading cached value for variable \'{0}\'. Time since pickling {1}'
78+
logging.warning('loading cached value for variable \'{0}\'. Time since pickling {1}'
7879
.format(str(var_name), str(datetime.datetime.now() - info["store_date"])))
7980
user_ns[var_name] = stored_value
8081
except IOError:
@@ -84,7 +85,7 @@ def __call__(self, version="*", reset=False, var_name="", var_value="", show_all
8485
raise CacheCallException("variable '" + str(var_name) + "' not in cache")
8586

8687
if var_value and stored_value is None:
87-
print('creating new value for variable \'' + str(var_name) + '\'')
88+
logging.warning('creating new value for variable \'' + str(var_name) + '\'')
8889
self._create_new_value(
8990
self.shell,
9091
var_folder_path,
@@ -145,7 +146,7 @@ def _show_all(base_dir):
145146
for subdir in os.listdir(base_dir):
146147
var_name = subdir
147148
if debug:
148-
print("found subdir: " + var_name)
149+
logging.warning("found subdir: " + var_name)
149150

150151
data_path = os.path.join(base_dir, var_name, "data.txt")
151152
size = os.path.getsize(data_path)
@@ -156,7 +157,7 @@ def _show_all(base_dir):
156157
vars.append([var_name, size, info["store_date"], info["version"], info["expression_hash"]])
157158

158159
except IOError:
159-
print("Warning: failed to read info variable '" + var_name + "'")
160+
logging.warning("Warning: failed to read info variable '" + var_name + "'")
160161

161162
display(HTML(tabulate(vars, headers=["var name", "size(byte)", "stored at date", "version", "expression(hash)"],
162163
tablefmt="html")))
@@ -179,11 +180,11 @@ def _handle_cache_hit(info, var_value, var_folder_path, version):
179180
if str(info["version"]) != str(version):
180181
# Note: Version can be a string, a number or the content of a variable (which can by anything)
181182
if debug:
182-
print("resetting because version mismatch")
183+
logging.warning("resetting because version mismatch")
183184
CacheCall.reset_folder(var_folder_path)
184185
elif info["expression_hash"] != CacheCall.hash_line(var_value):
185-
print("Warning! Expression has changed since last save, which was at " + str(info["store_date"]))
186-
print("To store a new value, change the version ('-v' or '--version') ")
186+
logging.warning("Warning! Expression has changed since last save, which was at " + str(info["store_date"]))
187+
logging.warning("To store a new value, change the version ('-v' or '--version') ")
187188
else:
188189
if version != '' and info['version'] != version:
189190
# force a version
@@ -203,8 +204,8 @@ def _get_cache_version(version_param, var_value, user_ns):
203204
if version_param.isdigit():
204205
return int(version_param)
205206

206-
print("Version: " + str(version_param))
207-
print("version_param.isdigit(): " + str(version_param.isdigit()))
207+
logging.warning("Version: " + str(version_param))
208+
logging.warning("version_param.isdigit(): " + str(version_param.isdigit()))
208209
raise CacheCallException("Invalid version. It must either be an Integer, *, or the name of a variable")
209210

210211
@staticmethod
@@ -223,7 +224,7 @@ def cache(self, line):
223224
parameter = self.parse_input(line)
224225
CacheCall(self.shell)(**parameter)
225226
except CacheCallException as e:
226-
print("Error: " + str(e))
227+
logging.error("Error: " + str(e))
227228

228229
@staticmethod
229230
def parse_input(_input):
@@ -298,6 +299,6 @@ def parse_input(_input):
298299
try:
299300
ip = get_ipython()
300301
ip.register_magics(CacheMagic)
301-
print("%cache magic is now registered in ipython")
302+
logging.warning("%cache magic is now registered in ipython")
302303
except:
303-
print("Error! Couldn't register magic in ipython!!!")
304+
logging.error("Error! Couldn't register magic in ipython!!!")

0 commit comments

Comments
 (0)