简体   繁体   中英

Can you use namedtuples/dataclass/attrs to create a flexible sized class or do you have to use traditional classes?

If there is a json data set that looks like this.

[
{'a':1,'b':'fire','c':'cambodia','type':'charizard'},
{'a':2,'d':'waterparks','type':'squirtle'},
{'a':3,'f':'thunder','type':'pikachu'}
]

And it is needed to transition it into a set of objects where the objects can be defined with the same class like this.

charizard = Pokemon(row_data)
pickachu = Pokemon(row_data)
squirtle = Pokemon(row_data)

But the attributes are accessible via dot notation, like this.

charizard.a
pikachu.d
squirtle.a

The way to do that with traditional classes is like this.

class Pokemon(object):
    def __init__(self, data):
        for k, v in data.items():
            setattr(self, k, v)

Is there a way to do basically the same thing with either namedtuples or dataclasses or attrs that works for data of different sizes and has all the nice immutability, repr, etc. functionality of those data types.

The point of attrs et al is to have well-defined classes for your data. So the short answer to your question is “no”.

Setting attributes dynamically like you you want to doesn't buy you much, except that it's less typing to access your data.

A better way is to have well-defined classes where one look tells you what attributes to expect and serialize/normalize your JSON into it. There's a bunch of packages for that; for attrs there's cattrs for instance. Or you just write a function or class method.

The time you spend making this transition explicit, you'll get back tenfold when debugging later.

Use attrs for data with schema. If your data DOES have a schema (even if it includes a whole lot of attributes), yes you can use attrs and you can even define data schema dynamically. But if your data is schemaless, don't define schema on it.

If you can give a few real examples (at least 10 of them), maybe that will help us understand your exact situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM