Skip to content

Commit

Permalink
Merge pull request #638 from Andrew-S-Rosen/patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
shyuep authored Mar 17, 2024
2 parents 4d60fd4 + 5fbd4d7 commit f5e3bf6
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions monty/itertools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Additional tools for iteration.
"""

import itertools

try:
Expand Down
7 changes: 5 additions & 2 deletions monty/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,8 +713,11 @@ def jsanitize(
except TypeError:
pass

if recursive_msonable and isinstance(obj, MSONable):
return obj.as_dict()
if recursive_msonable:
try:
return obj.as_dict()
except AttributeError:
pass

if not strict:
return str(obj)
Expand Down
1 change: 1 addition & 0 deletions monty/operator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Useful additional functions for operators
"""

import operator


Expand Down
1 change: 1 addition & 0 deletions monty/os/path.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Path based methods, e.g., which, zpath, etc.
"""

import os

from monty.fnmatch import WildCard
Expand Down
1 change: 1 addition & 0 deletions monty/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
This module implements serialization support for common formats such as json
and yaml.
"""

import json
import os

Expand Down
1 change: 1 addition & 0 deletions monty/shutil.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Copying and zipping utilities. Works on directories mostly."""

from __future__ import annotations

import os
Expand Down
1 change: 1 addition & 0 deletions monty/subprocess.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Calling shell processes.
"""

import shlex
import threading
import traceback
Expand Down
1 change: 1 addition & 0 deletions monty/termcolor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
ANSII Color formatting for output in terminal.
"""

import os

try:
Expand Down
1 change: 0 additions & 1 deletion tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
Deployment file to facilitate releases of monty.
"""


import datetime
import glob
import json
Expand Down
4 changes: 3 additions & 1 deletion tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,17 +600,19 @@ def test_jsanitize(self):
assert clean["world"] is None
assert json.loads(json.dumps(d)) == json.loads(json.dumps(clean))

d = {"hello": GoodMSONClass(1, 2, 3)}
d = {"hello": GoodMSONClass(1, 2, 3), "test": "hi"}
with pytest.raises(TypeError):
json.dumps(d)
clean = jsanitize(d)
assert isinstance(clean["hello"], str)
clean_strict = jsanitize(d, strict=True)
assert clean_strict["hello"]["a"] == 1
assert clean_strict["hello"]["b"] == 2
assert clean_strict["test"] == "hi"
clean_recursive_msonable = jsanitize(d, recursive_msonable=True)
assert clean_recursive_msonable["hello"]["a"] == 1
assert clean_recursive_msonable["hello"]["b"] == 2
assert clean_recursive_msonable["test"] == "hi"

d = {"dt": datetime.datetime.now()}
clean = jsanitize(d)
Expand Down

0 comments on commit f5e3bf6

Please sign in to comment.