Attribute error: 'list' object has no attribute 'items'
See original GitHub issueExample
main.py
import json
from dataclasses import dataclass, field
from dataclasses_json import dataclass_json, config
from typing import List, Dict, Any
@dataclass_json
@dataclass
class Coef:
val: float = field(default=float, metadata=config(field_name="C"))
cf_type: float = field(default=int, metadata=config(field_name="G"))
total: float = field(default=float, metadata=config(field_name="T"))
@dataclass_json
@dataclass
class QuarterCoefs:
coefs: List[Coef] = field(default_factory=list,
metadata=config(field_name="E"))
num: str = field(default="", metadata=config(field_name="PN"))
@dataclass_json
@dataclass
class Game:
quarter_coefs: QuarterCoefs = field(default_factory=list,
metadata=config(field_name="SG"))
with open('test.json', 'r') as f:
match = json.load(f)
game_json = json.dumps(match)
m = Game.from_json(game_json)
print(m)
test.json
{
"SG": [{
"E": [{
"C": 1.71,
"G": 1,
"T": 1
},
{
"C": 9,
"G": 1,
"T": 2
},
{
"C": 2.4,
"G": 1,
"T": 3
}
],
"PN": "2 Quarter"
}]
}
Error
raceback (most recent call last):
File "/Users/peq/code/vk/bball_bot/test.py", line 35, in <module>
m = Game.from_json(game_json)
File "/usr/local/lib/python3.7/site-packages/dataclasses_json/api.py", line 110, in from_json
return cls.from_dict(kvs, infer_missing=infer_missing)
File "/usr/local/lib/python3.7/site-packages/dataclasses_json/api.py", line 117, in from_dict
return _decode_dataclass(cls, kvs, infer_missing)
File "/usr/local/lib/python3.7/site-packages/dataclasses_json/core.py", line 166, in _decode_dataclass
infer_missing)
File "/usr/local/lib/python3.7/site-packages/dataclasses_json/core.py", line 109, in _decode_dataclass
kvs = {decode_names.get(k, k): v for k, v in kvs.items()}
AttributeError: 'list' object has no attribute 'items'
What I’m doing wrong?
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Why I get 'list' object has no attribute 'items'? - Stack Overflow
You have a dictionary within a list. You must first extract the dictionary from the list and then process the items in the...
Read more >AttributeError: 'list' object has no attribute 'values' - Python Forum
The error is from trying to access a list like a dict. you can use the list index to access the data or...
Read more >AttributeError: list object has no attribute items ( Solved )
AttributeError : list object has no attribute items error occurs because of accessing items() function from list object in the place of dict...
Read more >'list' object has no attribute 'items' (Example) - Treehouse
Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest...
Read more >AttributeError: 'list' object has no attribute 'values'
AttributeError : 'list' object has no attribute 'values' ... The error seems to be raised in ctx.quantum_circuit.run which seems to be another library....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
When I get stuck on something like this I create the object and then serialize it to make sure I have all the [{}] in the right places. In this case it looks like you are missing one set of {}.
game = Game(QuarterCoefs([Coef(1.71, 1, 1), Coef(9, 1, 2), Coef(2.4, 1, 3)], “2 Quarters”)) print(Game.to_json(game, indent=2))
{ “SG”: { “E”: [ { “C”: 1.71, “G”: 1, “T”: 1 }, { “C”: 9, “G”: 1, “T”: 2 }, { “C”: 2.4, “G”: 1, “T”: 3 } ], “PN”: “2 Quarters” } }
On Fri, Aug 23, 2019 at 9:27 AM Maxim Lyubiev notifications@github.com wrote:
– Nathan Atkins 720.346.8292 atkinsnw@gmail.com linkedin.com/in/nathan-atkins
Maxim,
Sorry for looking at the backwards yesterday. I think this does what you want. If not ping me back. Note that last pass at it uses the Marshmallow Schema and actually gets the coef’s as floats. I’m not sure if you are trying to use the default=float to handle missing values in some way, but if I drop one of those fields it blows up. If you need to handle missing values let me know and I can see if I can figure that out.
import json
from dataclasses import dataclass, field
from dataclasses_json import dataclass_json, config
from typing import List, Dict, Any
@dataclass_json
@dataclass
class Coef:
@dataclass_json
@dataclass
class QuarterCoefs:
@dataclass_json
@dataclass
class Game:
field(metadata=config(field_name=“SG”))
target = {
2.4, “G”: 1, “T”: 3}], “PN”: “2 Quarter”} ]
}
if name == “main”:
1, 3)], “2 Quarter”)])
all the coef’s are floats
{“SG”: [{“E”: [{“C”: 1.71, “G”: 1, “T”: 1}, {“C”: 9, “G”: 1, “T”: 2}, {“C”: 2.4, “G”: 1, “T”: 3}], “PN”: “2 Quarter”}]}
{“SG”: [{“E”: [{“C”: 1.71, “G”: 1, “T”: 1}, {“C”: 9, “G”: 1, “T”: 2}, {“C”: 2.4, “G”: 1, “T”: 3}], “PN”: “2 Quarter”}]}
{“SG”: [{“E”: [{“C”: 1.71, “T”: 1.0, “G”: 1.0}, {“C”: 9.0, “T”: 2.0, “G”: 1.0}, {“C”: 2.4, “T”: 3.0, “G”: 1.0}], “PN”: “2 Quarter”}]}
On Fri, Aug 23, 2019 at 12:23 PM Nathan Atkins atkinsnw@gmail.com wrote:
– Nathan Atkins 720.346.8292 atkinsnw@gmail.com linkedin.com/in/nathan-atkins