From c3fd529287c4208fd739dab0c0d86e3637bfb144 Mon Sep 17 00:00:00 2001 From: Jakub Kuszneruk Date: Tue, 30 Aug 2022 13:37:59 +0200 Subject: [PATCH] Log argparse.Namespace objects as dicts (#984) --- CHANGELOG.md | 1 + .../metadata_containers/metadata_container.py | 3 +++ tests/neptune/new/test_handler.py | 24 +++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1101572f..d9c18fa81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Features - Added support for Prophet integration ([#978](https://github.com/neptune-ai/neptune-client/pull/978)) +- Log argparse.Namespace objects as dicts ([#984](https://github.com/neptune-ai/neptune-client/pull/984)) ## neptune-client 0.16.5 diff --git a/neptune/new/metadata_containers/metadata_container.py b/neptune/new/metadata_containers/metadata_container.py index 969ff9a7a..f1323038f 100644 --- a/neptune/new/metadata_containers/metadata_container.py +++ b/neptune/new/metadata_containers/metadata_container.py @@ -14,6 +14,7 @@ # limitations under the License. # import abc +import argparse import atexit import itertools import threading @@ -256,6 +257,8 @@ def define( pass elif isinstance(value, Handler): value = ValueCopy(value) + elif isinstance(value, argparse.Namespace): + value = Namespace(vars(value)) elif is_bool(value): value = Boolean(value) elif is_int(value): diff --git a/tests/neptune/new/test_handler.py b/tests/neptune/new/test_handler.py index 420c2b4ca..0227313f2 100644 --- a/tests/neptune/new/test_handler.py +++ b/tests/neptune/new/test_handler.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import argparse import os import unittest @@ -404,6 +405,29 @@ def test_assign_dict(self): self.assertEqual(exp["params/toys"].fetch_last(), "hat") self.assertEqual(exp["params/nested/nested/deep_secret"].fetch_last(), 15) + def test_convertable_to_dict(self): + exp = init(mode="debug", flush_period=0.5) + exp["params"] = argparse.Namespace( + foo="bar", baz=42, nested=argparse.Namespace(nested_attr=[1, 2, 3], num=55) + ) + self.assertEqual(exp["params/foo"].fetch(), "bar") + self.assertEqual(exp["params/baz"].fetch(), 42) + self.assertEqual(exp["params/nested/nested_attr"].fetch(), "[1, 2, 3]") + self.assertEqual(exp["params/nested/num"].fetch(), 55) + + def test_object(self): + class Dog: + def __init__(self, name, fierceness): + self.name = name + self.fierceness = fierceness + + def __str__(self): + return f"{self.name} goes " + "Woof! " * self.fierceness + + exp = init(mode="debug", flush_period=0.5) + exp["burek"] = Dog("Burek", 3) + self.assertEqual(exp["burek"].fetch(), "Burek goes Woof! Woof! Woof! ") + def test_artifacts(self): exp = init(mode="debug", flush_period=0.5) exp["art1"].track_files("s3://path/to/tracking/file", destination="/some/destination")