How to get singular value from a generator?

Lorenzo Ang

I'm writing an app to monitor chicken deaths in a poultry farm and have the following code:

today = datetime.date.today()
deaths = (sum(cd.count) for cd in ChickenDeath.objects.filter(date__gte=datetime.date(today.year, today.month, 1)))
print(deaths)

My problem is it just prints out <generator object chickens_overview.<locals>.<genexpr> at 0x110010678> instead of the actual sum. I've tried next(deaths) as well however I just get the error 'int' object not iterable. Is there another way to get the value from this generator?

Daniel Roseman

You shouldn't be returning a generator at all here. The generator should be the input to the sum function.

At the moment, you're effectively creating a list of items, each of which is the "sum" of a single value. That's why you get the error about not iterable; it's cd.count that is not iterable, because it's a single value and you can't sum that.

I suspect what you meant was to sum all the counts:

deaths = sum(cd.count for cd in ChickenDeath.objects.filter(...))

but that is not the right thing to do. You should ask the database to do the sum for you:

from django.db.models import Sum
deaths = ChickenDeath.objects.filter(...).aggregate(total=Sum('count'))
print(deaths['total'])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get one value at a time from a generator function in Python?

How to get a value from map

How to get value of generator

How can I force the singular table name on Rails scaffold generator?

How to invert numpy matrices using Singular Value Decomposition?

How to interpret Singular Value Decomposition results (Python 3)?

Singular value decomposition in R

How to get the value from a tensor

store a value from a random generator

Is there a way for one method to get the next value from a generator in another method?

How can I get value from user for click function using if statement for a password generator?

How can I update singular value of Array in React State

Get an iterable generator from an async generator

How to intercept the first value of a generator and transparently yield from the rest

How to get value from a button?

How can I yield a value from within a callback to a calling generator

How to get the nth value of a JavaScript generator?

Get single yield value from iterator/generator

How to get a singular value from a list of tuples?

How to get value from type

How to get rid of duplicates from random name generator?

Vue JS time-slots-generator how to get value from object then display PM / AM

how to get the value from an xpath

How to merge two individuals cells into a singular combined value in Pandas

Get the generator function's name from a generator

How to pass or get the value from Map (random generator) to PebbleStringBody in Gatling with Scala

Get functions from python generator

How to assign a Singular Keys Array to Multiple Nested Value Arrays

How to make simple python generator from nested generator, "Generator in Generator"?