[Tutor] Unnecessary comprehension warning
boB Stepp
robertvstepp at gmail.com
Sun Jun 7 01:24:35 EDT 2020
On Sat, Jun 6, 2020 at 11:15 PM boB Stepp <robertvstepp at gmail.com> wrote:
>
> On Sat, Jun 6, 2020 at 10:14 PM DL Neil via Tutor <tutor at python.org> wrote:
> >
> > On 7/06/20 3:00 PM, boB Stepp wrote:
> > > On Sat, Jun 6, 2020 at 9:29 PM DL Neil via Tutor <tutor at python.org> wrote:
> > >>
> > >> On 7/06/20 2:19 PM, boB Stepp wrote:
> > >>> Continuing "Exercise 3.12: Using your library module" in "3.4 Modules" at
> > >>> https://dabeaz-course.github.io/practical-python/Notes/03_Program_organization/04_Modules.html.
> > >>>
> > >>>
> > >>> I rewrote another function in report.py, read_prices(), to make use of the
> > >>> parse_csv() function in the fileparse module. The rewritten function is:
> > >>>
> > >>> def read_prices(filename: str) -> Dict[str, float]:
> > >>> """Read a file of stock prices and load them into a dictionary."""
> > >>> prices = fileparse.parse_csv(filename, types=[str, float],
> > >>> has_headers=False)
> > >>> stock_prices = {stock_name: stock_price for stock_name, stock_price
> > >>> in prices}
> > >>> return stock_prices
> > >>>
> > >>> pylint complains with:
> > >>>
> > >>> report.py|25 col 1 warning| unnecessary-comprehension: Unnecessary use
> > >>> of a comprehension
> > >>>
> > >>> The dictionary comprehension I'm now using replaces a lengthier for loop
> > >>> with indexing instead of nice names. Am I truly doing things in a bad
> > >>> Python style as the linter is complaining? Should I be doing this
> > >>> differently?
Finally figured it out. The linter was correct! I should just return
the following:
return dict(fileparse.parse_csv(fileobj, types=[str, float], has_headers=False))
Doh!
--
boB
More information about the Tutor
mailing list