Home > Software engineering >  Python - Dataclass: load attribute value from a dictionary containing an invalid name
Python - Dataclass: load attribute value from a dictionary containing an invalid name

Time:05-02

Unfortunately I have to load a dictionary containing an invalid name (which I can't change):

dict = {..., "invalid-name": 0, ...}

I would like to cast this dictionary into a dataclass object, but I can't define an attribute with this name.

from dataclasses import dataclass

@dataclass
class Dict:
    ...
    invalid-name: int  # can't do this
    ...

The only solution I could find is to change the dictionary key into a valid one right before casting it into a dataclass object:

dict["valid_name"] = dict.pop("invalid-name")

But I would like to avoid using string literals...

Is there any better solution to this?

CodePudding user response:

One solution would be using dict-to-dataclass. As mentioned in its documents it has two options:

1.passing dictionary keys

It's probably quite common that your dataclass fields have the same names as the dictionary keys they map to but in case they don't, you can pass the dictionary key as the first argument (or the dict_key keyword argument) to field_from_dict:

@dataclass
class MyDataclass(DataclassFromDict):
    name_in_dataclass: str = field_from_dict("nameInDictionary")

origin_dict = {
    "nameInDictionary": "field value"
}

dataclass_instance = MyDataclass.from_dict(origin_dict)

>>> dataclass_instance.name_in_dataclass
"field value"
  1. Custom converters

If you need to convert a dictionary value that isn't covered by the defaults, you can pass in a converter function using field_from_dict's converter parameter:

def yes_no_to_bool(yes_no: str) -> bool:
    return yes_no == "yes"


@dataclass
class MyDataclass(DataclassFromDict):
    is_yes: bool = field_from_dict(converter=yes_no_to_bool)

dataclass_instance = MyDataclass.from_dict({"is_yes": "yes"})

>>> dataclass_instance.is_yes
True

CodePudding user response:

The following code allow to filter the nonexistent keys :

@dataclass
class ClassDict:
    valid-name0: str
    valid-name1: int  
    ...

dict = {..., "invalid-name": 0, ...}

dict = {k:v for k,v in dict.items() if k in ClassDict.__dict__["__dataclass_fields__"].keys()}

However, I'm sure there should be a better way to do it since this is a bit hacky.

CodePudding user response:

I would define a from_dict class method anyway, which would be a natural place to make the change.

@dataclass
class MyDict:

    ...
    valid_name: int
    ...

    @classmethod
    def from_dict(cls, d):
        d['valid_name'] = d.pop('invalid-name')
        return cls(**d)


md = MyDict.from_dict({'invalid-name': 3, ...})

Whether you should modify d in place or do something to avoid unnecessary copies is another matter.

  • Related