Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for tuples, namedtuples, OrderedDict and possibly others #89

Open
davidwaroquiers opened this issue Dec 20, 2019 · 1 comment
Open

Comments

@davidwaroquiers
Copy link
Contributor

Summary

In some objects, tuples, namedtuples, OrderedDict and other data-container objects are used. Currently, MontyEncoder and MontyDecoder do not support them (it actually converts tuples to lists, OrderedDicts to dicts)

  • When decoding back these objects, they are not anymore the same type of data object and this could be a problem in some cases.
  • For OrderedDict, it might not be such of a problem as dicts are now guaranteed to be ordered in python 3.7, if I'm not wrong.

Is there any plan to implement such a feature ? Would it be useful for people ?

Example code for namedtuple

from collections import namedtuple
from monty.json import MontyEncoder
from monty.json import MontyDecoder

me = MontyEncoder()
md = MontyDecoder()

Point = namedtuple('Point', field_names=['x', 'y'])
a = Point(1, 2)

mydict = {'point': a}
mydecodeddict = md.decode(me.encode(mydict))

print(mydict == mydecodeddict) # Returns False

Suggested solution (if known)

  • Deal with these specific data objects in a similar way numpy is handled.
@shyuep
Copy link
Contributor

shyuep commented Dec 22, 2019

I have no problems with this. Feel free to implement and submit a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants