[Tutor] Playing with generators

avi.e.gross at gmail.com avi.e.gross at gmail.com
Wed Aug 10 11:08:18 EDT 2022


Not sure if this question is another IQ test from someone! LOL!

Overview, once I struggled with your naming is that you have data in a
dictionary format and a list of such items. You want to convert each
dictionary to an object of Class Person and then instead of a list of Person
objects want to make a dictionary of Person objects as values with keys
specific to that object. So far, seems easy enough but you have an amorphous
question about wanting to use generators as an exercise.

Generally, a generator does not make everything at once but keeps returning
more as it is iterated. It can return a next item or it can return a growing
item, so what exactly is your deliverable here? Do you want a generator that
makes one Person at a time as needed, or one dictionary containing a person
with a key at a time, or a growing dictionary of such dictionaries that is
lengthened by one each time?

As a start, I  just want to make a suggestion so your code is a tad easier
to understand.

Your make_person() function does not make A person but a list of objects of
class Person. Consider naming it make_persons() to indicate that. Yes, it
uses a generator expression and arguably makes one at a time. But the result
from outside seems more naturally, to me, to be a generator that makes
persons.

Your uses of the name "peeps" took a while to make sense and now seems to be
a slang way of saying "peoples" and may be intuitively obvious to you but
made me start thinking of other things to do than read your uncommented code
that made me have to figure out what it was you were doing.

You seem to want to make a dictionary of People as values indexed by first
name. First names tend to not be unique.  So not an optimal key unless you
don't care or have some guarantees.

But if I am getting your question, and I bet I am not, you want to know if
there is a briefer or simpler way to consolidate what you are doing. Will
this work?

def make_peeps(data):
    return { datum.get('fname', 'missing'): Person(datum) for datum in data
}

The above just needs your list of dictionaries, stored in "data" and uses a
dictionary comprehension to make the entire dictionary of Person objects you
seem to want. But are you requiring this be done in some kind of generator
for a practical reason or just as an exercise?

One obvious way to do it is with a generator function with an explicit
yield. Put your loop in there and yield whatever you want until done. Or do
you insist on using a generator expression way?



-----Original Message-----
From: Tutor <tutor-bounces+avi.e.gross=gmail.com at python.org> On Behalf Of
Leam Hall
Sent: Wednesday, August 10, 2022 7:33 AM
To: python tutor <Tutor at python.org>
Subject: [Tutor] Playing with generators

I'm wondering if there's a way to combine "make_person" with the generator
in "for fname, peep in". Even if it can be done, should it be done? Or would
the code be overly complex?

The goal is to parse a group of data, and create a dict where the value is
an object created from the data, and the key is an attribute of the object
itself.


class Person():
     def __init__(self, data = {}):
         self.fname = data.get('fname', 'fred')
         self.lname = data.get('lname', 'frank')

data = [
     { 'fname':'Wilbur', 'lname':'Lefron' },
     { 'fname':'Al', 'lname':'Lefron' },
     { 'fname':'Jo', 'lname':'Franco' },
     { 'fname':'Mon', 'lname':'Pascal' },
     { 'fname':'Gray', 'lname':'Webb-Marston'},
     ]

def make_person(data):
     ps = ( Person(d) for d in data)
     return ps
         
peeps = {}

for fname, peep in ( (p.fname, p) for p in make_person(data) ):
     peeps[fname] = peep


for person in peeps.values():
     print(person.lname)

-- 
Automation Engineer        (reuel.net/resume)
Scribe: The Domici War     (domiciwar.net)
General Ne'er-do-well      (github.com/LeamHall)
_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list