Skip to content

Commit

Permalink
Log argparse.Namespace objects as dicts (#984)
Browse files Browse the repository at this point in the history
  • Loading branch information
shnela authored Aug 30, 2022
1 parent d31d589 commit c3fd529
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions neptune/new/metadata_containers/metadata_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.
#
import abc
import argparse
import atexit
import itertools
import threading
Expand Down Expand Up @@ -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):
Expand Down
24 changes: 24 additions & 0 deletions tests/neptune/new/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import argparse
import os
import unittest

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit c3fd529

Please sign in to comment.