def test_logging_support(self): """ Check that the logging support functions can be called without error. """ Logger.indent() Logger.clear_indent() Logger.line_break() Logger.unindent() Logger.ruler()
def test_add_action(self): """ Test that we can add an arbitrary action to the global logger. """ raw_message = "Hello World" def my_action(message: str, _: bool): self.assertTrue(raw_message in message) Logger.add_action("tag", my_action) Logger.log(raw_message)
def tearDown(self): """ Remove all the artifact files we may have written. """ if os.path.exists(self.as_log_file(self.LOCAL_OUT_FILE)): os.remove(self.as_log_file(self.LOCAL_OUT_FILE)) if os.path.exists(self.as_log_file(self.GLOBAL_OUT_FILE)): os.remove(self.as_log_file(self.GLOBAL_OUT_FILE)) if os.path.exists(self.ERROR_FILE): os.remove(self.ERROR_FILE) Logger.clear_actions()
def __init__(self): # Create the full paths. settings_example_path = f"{self.K_SETTINGS_ROOT}/{self.K_SETTINGS_EXAMPLE_FILE}" settings_path = f"{self.K_SETTINGS_ROOT}/{self.K_SETTINGS_FILE}" # If local settings doesn't exist, then copy it from the example. if not os.path.exists(settings_path): shutil.copy2(settings_example_path, settings_path) # Read the settings yaml. with open(settings_path, 'r') as f: self.data = yaml.load(f) # Print the loaded settings. Logger.special("Settings Initialized") for k, v in self.data.items(): Logger.field(k, v)
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Loads a bunch of data from the directory. """ from k_util.logger import Logger from ai.predictor import Predictor from data.loader import load_training_samples from eval.evaluator import evaluate if __name__ == "__main__": samples = load_training_samples() # Predict all predictor = Predictor() for s in samples: s.predictions = predictor.predict(s) Logger.log("Samples Predicted") # Evaluate the samples. evaluate(samples)
def test_log_file_overwrite(self): """ Check that attaching a file handler will clear the previous one. """ # Create the first file. logger = Logger() logger.attach_file_handler(".", self.LOCAL_OUT_FILE) logger.log("1") logger.log("2") logger.log("3") logger.error("e1") logger.error("e2") # Now the file should be overwritten. logger = Logger() logger.attach_file_handler(".", self.LOCAL_OUT_FILE) logger.log("1") logger.error("e1") with open(self.as_log_file(self.LOCAL_OUT_FILE)) as f: self.assertEqual(1, len(f.readlines())) with open(self.ERROR_FILE) as f: self.assertEqual(1, len(f.readlines()))
def test_file_attach_handler(self): """ Test that a file handler can be attached, and that the logger will produce content to it. """ # Test that we can attach and write to a local output file. logger = Logger() logger.attach_file_handler(".", self.LOCAL_OUT_FILE) logger.log("Hello") logger.log("World") self.assertTrue(os.path.exists(self.as_log_file(self.LOCAL_OUT_FILE))) # Test that we can attach and write to a global output file. Logger.attach_file_handler(".", self.GLOBAL_OUT_FILE) Logger.log("How are you?") self.assertTrue(os.path.exists(self.as_log_file(self.GLOBAL_OUT_FILE))) # Test that these two files have the correct line length. with open(self.as_log_file(self.LOCAL_OUT_FILE)) as f: self.assertEqual(2, len(f.readlines())) with open(self.as_log_file(self.GLOBAL_OUT_FILE)) as f: self.assertEqual(1, len(f.readlines())) # Test error logging. Logger.error("This is an error") self.assertTrue(os.path.exists(self.ERROR_FILE)) with open(self.ERROR_FILE) as f: self.assertEqual(1, len(f.readlines()))
def test_local_logging(self): """ Test that we can create a local logging instance that is separate from the singleton. """ logger = Logger() logger.log("This is a message from the local logger.") Logger.log("This is a message from the global logger.") self.assertNotEqual(logger, Logger.instance())
def test_global_logger(self): """ Check that the global (singleton) logger can be initialized and used without error. """ Logger.log("This is a standard log message.") Logger.header("This is a header.") Logger.special("This is a special.") Logger.field("This is", "a field.") Logger.field("This is", "a red field.", red=True) Logger.error("This is an error.")
def test_progress(self): """ Simply test if the function can be called. """ Logger.progress(0.5)
#!/usr/bin/env python # -*- coding: utf-8 -*- """ <ENTER DESCRIPTION HERE> """ from k_util.logger import Logger __author__ = "Jakrin Juangbhanich" __email__ = "*****@*****.**" __version__ = "0.0.0" if __name__ == "__main__": Logger.header("Running Tools Test") Logger.field("Version", "1.2.0") Logger.log("Hello World") Logger.line_break()
@show_image def region_label(image: np.array, display_text: str): """ Write text into the specified region, with the anchor. """ region = Region(100, 320, 40, 130) cv2.rectangle(image, (region.left, region.top), (region.right, region.bottom), color=(0, 255, 0), thickness=1) return text.label_region(image=image, text=display_text, region=region, icon=u"\uf447", show_at_bottom=True) # ====================================================================================================================== # Run the script. # ====================================================================================================================== if __name__ == "__main__": Logger.header("Running Text_Example") # Load the default image to draw on. module_dir = os.path.dirname(__file__) file_path = os.path.join(module_dir, "stars.jpeg") print(file_path) base_image = cv2.imread(file_path) plain_text(base_image, "Plain Text") big_text(base_image, "BIG TEXT") small_text(base_image, "small text") region_text_left(base_image, "Region Left") region_text_right(base_image, "Region Right") region_with_bg(base_image, "Region With BG") region_with_clear_bg(base_image, "Clear BG") icon_raw(base_image, u"\uf447")