question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add public BaseModel.iter() method

See original GitHub issue

Feature Request

Add BaseModel.iter() method which will behave exactly like a dict, but without converting nested values to dict by default and will be a generator instead

Why

Imagine you want to iterate through values in model, but also need to exclude some, or use aliases instead of attributes. So what you need to do is write something like this:

for attr, value in model.dict(exclude={...}, by_alias=True).items():
    do_something(attr, value)

Obviously, in this case python will firet evaluate model.dict(exclude={...}, by_alias=True). part, going through all values and only then you will be able to iterate, going through the same values again.

Also, by using dict nested values are force-converted to dict, and this might be very slow when we just want to iterate through top-level values.

We also cannot use existing _iter(), because some work is actually done in dict()

How we can solve it

By having iter() method we can bypass this double-iteration problem and converting-to-dict problems:

for attr, value in model.iter(exclude={...}, by_alias=True):
    do_something(attr, value)

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
samuelcolvincommented, Nov 14, 2019

This already exists!

It’s called ._iter().

1reaction
dmontagucommented, Nov 14, 2019

For what it’s worth, I would be in favor of moving logic from dict into _iter, assuming it didn’t result in meaningful performance penalties in any public methods – I think it would simplify/result in a smaller diff for the implementation of dump_as_type from #812, and the related serialization performance improvements (even when not dumping as a specified type).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add public BaseModel.iter() method #997 - pydantic ... - GitHub
Add BaseModel.iter() method which will behave exactly like a dict, but without converting nested values to dict by default and will be a ......
Read more >
Python/Pydantic iterate over post body with multiple items
How can I iterate the single entries from the post body? import uvicorn from pydantic import BaseModel from fastapi import FastAPI from typing ......
Read more >
Models - pydantic
Models possess the following methods and attributes: dict(): returns a dictionary of the model's fields and values; cf. exporting models; json(): returns a ......
Read more >
Source code for pandasdmx.model
"""SDMX Information Model (SDMX-IM). This module implements many of the classes described in the SDMX-IM specification ('spec'), which is available from: ...
Read more >
Comparing Python and JavaScript: A Guide for Developers
Magic methods such as __init__ use "dunder" names (for double underscore) ... You can also enter the python command to start the REPL...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found