Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why strings are not read correctly using easydict?

I use the easydict library to set the configurations, but when I store the configuration as strings, it turns the string into a list of the string itself, as follows:

from easydict import EasyDict
conf = EasyDict()
conf.name = 'CD Group'
print(conf.name)

> ['CD Group']    # a list

But what I hope to achieve is:

> CD Group    # string type

Why is it like this?
Thanks for any feedback!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

easydict source

An interesting point is that when I pass the configuration from the argparse into the easydict, the type doesn’t get an error, as follow:

conf.file_name = args.file_name    # --file_name input.xlsx
print(conf.file_name)

> input.xlsx

>Solution :

looking at __setattr__

    def __setattr__(self, name, value):
        if isinstance(value, (list, tuple)):
            value = [self.__class__(x)
                     if isinstance(x, dict) else x for x in value]
        elif isinstance(value, dict) and not isinstance(value, self.__class__):
            value = self.__class__(value)

as you can see in __setattr__ method , if you pass a variable as list or tuple , you get a list, if not you get the original type back.

and I don’t have the problem you are mentioning :

conf = EasyDict()
conf.name = 'CD Group'
conf.name2 = ['CD Group']
print(conf.name , conf.name2, sep='\n')

output:

>>
CD Group
['CD Group']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading