0

enter image description hereI have a worksheet in google sheets where each row is information about an upcoming event, including the name, location, tickets etc...

I have a class Event and I'd like to create a new instance of Event for every row in the worksheet, but I want the name of the new Event objects to be values from the row.

Creating the class is easy enough because I can set attributes like

self.venue = worksheet.cell(foo,bar).value

But I'm not sure how to use the same logic for the name of the variable when creating the object

e.g.

worksheet.cell(row, column).value = Event(etc...) 

I need some way to say create this new Event and name the variable worksheet.cell(row, column).value for row in this worksheet

Edit: added example of the data I'm working with. The 'band' column is the one with the strings that I would like to use as names for newly created objects

2 Answers 2

3

There are many ways to do it. But the most recommended way to do so is using dictionaries and not just naming the objects dynamically. For example:

EventObjects = {}

# and for each row and col that you want to create the objec
EventObjects[worksheet.cell(row, column).value] = Event(etc..)

Then when you want to access it, you can just get the instance of the object as so:

EventObjects['<Name>'].time

The other (not recommended way) is to use setattr:

import sys

# Assuming that this code is just executed in main.
# If not, just substitute sys.modules['__main__'] with the instance in which
# you want to create the varible
# like for example:
# settar(self, 'a', 10) creates a variable self.a with value 10

# For every row and column
setattr(sys.modules['__main__'], worksheet.cell(row, column).value, Event(etc ..))
Sign up to request clarification or add additional context in comments.

Comments

1

You can set the class attributes by editing its __dict__:

Event.__dict__[worksheet.cell(row, column).value] = ...

(assuming worksheet.cell(row, column).value is a string).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.