1

I am using python 3.9

I have a class called ImageMetaData

class ImageMetadata(BaseModel):
 title: Optional[str] = None
 source: Optional[str] = None
 sourcesubtype: Optional[str] = None
 filesize: Optional[int] = None
 url: Optional[str] = None

and a function

def total_filesize(images: List[ImageMetadata]):
 total = 0
 try:
 for i in images:
 total = total + i.filesize
 except BaseException as ex:
 logger.error("Caught exception trying to compute total filesize for images", exc_info=True)
 return total

I get an error about AttributeError:

‘dict’ object has no attribute ‘filesize’

Am I missing something?

asked Dec 20, 2021 at 22:25
0

2 Answers 2

1

"filesize" is a dictionary key, not an attribute. Use i["filesize"], not i.filesize. This is not the same.

answered Dec 20, 2021 at 22:28
Sign up to request clarification or add additional context in comments.

Comments

0

You should modify the function total_filesize. Try like this:

def total_filesize(images: List[ImageMetadata]):
 total = 0
 
 try:
 for i in images:
 total = total + i["filesize"]
 
 except BaseException as ex:
 logger.error("Caught exception trying to compute total filesize for images", exc_info=True)
 return total
answered Dec 20, 2021 at 22:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.