0

I'm making a program that reads in data from a .ini file using configparser.py, so that I can create a Person object based on the information in the file.

My .ini's look like this:

[Pers1]
name = Alice

[Pers2]
name = Bob

...

[Pers25]
name = Xavier

I also have a class Person which takes the argument name, such that ExamplePerson = Person("Jim") creates a new Person object with the attribute ExamplePerson.name being "Jim".

So far, I've figured out how to read the .ini file, but I can't figure out how to write a function that take the file and manufacture objects from it.

I can manually create variables: Person1 = Person(name_from_ini_file), but I don't know how to write something like:

def make_objects_from_file(file_name):
    # read file_name, etc
    # get name from .ini file, store it as new_name
    # for item in file:
    PersX = Person(new_name)
    # PersX is Pers1 for the first object, then Pers2, and so on

So that I can call make_objects_from_file(persons.ini) and end up with Pers1 = Person("Alice"), Pers2 = Person("Bob"), ...Pers25 = Person("Xavier), or however many people are in the .ini file.

However, I don't how to create the initial Pers1 ... Per25 variables. I've looked into object factories, but either I'm misreading how they work or they aren't able to dynamically make more variables. I can't just type in Pers1 = ... , Pers2 = ... , Pers3 = ...; I need a way to make Pers1 ... in code. Not strings "Pers1", but actual variables.

This might help clarify:

NEW_OBJECT               =     Person(name)
    ^                            ^
I have no idea how to        I know exactly how to make this
make this
Somatic
  • 193
  • 7

2 Answers2

2

The idiomatic way to store a collection of named objects in Python is to use a dictionary, for you this might look something like

names_list = ["alice","bob"] #from your file
people = {"Pers"+str(i):Person(name) for i,name in enumerate(names_list)}
maxymoo
  • 35,286
  • 11
  • 92
  • 119
1

Don't try to create variable names dynamically. Instead, create keys in a dictionary.

people = {}
def make_objects_from_file(file_name):
    ...
    people["Pers1"] = Person(new_name)
    people["Pers2"] = Person(new_name)
    # etc
chepner
  • 497,756
  • 71
  • 530
  • 681
  • If I do this, can I make so the keys are objects, so I can call `Pers1.get_funds`, or `Pers5.do_a_thing`? – Somatic Jan 19 '16 at 03:39
  • 1
    @Somatic if you want to use the dot syntax you can use a `collections.namedtuple` – maxymoo Jan 19 '16 at 03:41
  • @maxymoo How would that help, OP already has a Person object defined? – AChampion Jan 19 '16 at 03:43
  • Wow... It's like something snapped in my brain and now I get it. I knew about dictionaries, and I've been using them, but I didn't comprehend how to make them do that until I saw that comment. – Somatic Jan 19 '16 at 03:45
  • @AChampion @maxymoo It would be very nice to be able to use dot syntax, since that's how my code is written right now and rewriting it to use `people[foo].get_funds()` would take a bit of effort, but I haven't put so much work into it that it would be impossible. – Somatic Jan 19 '16 at 03:47
  • @Somatic not sure how you code works with a variable number of people, if you are operating on the collection of people then you would iterate over the people using a common variable name, e.g. `for pers in people.values(): pers.get_funds()` – AChampion Jan 19 '16 at 04:02
  • @AChampion Yeah, my code was pretty ugly, but it worked okay as long as I manually assigned the variables, so `Pers1 = blah blah blah`. Now that I see how to make dictionaries work this way, I can fix the rest myself. – Somatic Jan 19 '16 at 04:05