Simplified, I want to do something like this:
({'publication': obj.pub_name, 'views': obj.views, } for obj = analyze_publication(p) for p in Publication.objects.all())
Of course, that doesn’t work.
Right now, I’m using:
({'publication': obj.pub_name, 'views': obj.views, } for obj in (analyze_publication(p) for p in Publication.objects.all()))
I have no idea if the second code piece is how it’s done or there’s another syntax, or it’s not efficient etc. I’m only 2 weeks into Python.
You
have unbalanced parens, but other than that, you should have no (functional) problem nesting generator expressions. As with list comprehensions, they quickly become unwieldy when nested. I’d recommend moving it out to a named generator, for readability’s sake.If you are curious about performance, compare different approaches using the disassembler or profilers.