I try to create an object by passing the variables, but it seems not work.
I put a simple example below to show what I want. Please help me to deal with this issue.
Successfully
temp = catalog.TEST
temp = catalog.PROD
Not works, it pass string "i" instead of the list element as attributes
lists = ['TEST,'PROD']
for i in lists:
temp = catalog.i
Complete code I'm using dremio-client this package (https://dremio-client.readthedocs.io/en/latest/readme.html)
import dremio_client as dc
mydremio = dc.init(os.getcwd() + '/.config')
catalog = mydremio.data
# TEST and PROD are the folders that exist in my dremio server
a = catalog.TEST
b = catalog.PROD
# Cannot pass the list element to object "catalog"
list = ["TEST","PROD"]
for i in list
temp = catalog.i
Thanks Pavel for the solution, but I have a one more complicated question.
list = ["TEST","TEST.DEMO"]
# what's work - directly declare
test = catalog.TEST.DEMO
# works for "TEST" not works for TEST.DEMO
for i in list:
temp = getattr(catalog, i)
When you do temp = catalog.i
, you actually trying to set an attribute i
of the catalog
, not value under variable i
.
You can try using getattr
instead:
import dremio_client as dc
mydremio = dc.init(os.getcwd() + '/.config')
catalog = mydremio.data
# TEST and PROD are the folders that exist in my dremio server
a = catalog.TEST
For the second case you can try to do something like this:
for i in list:
fields = i.split('.')
temp = catalog
for field in fields:
temp = getattr(temp, field)
What I do here is split i
by .
symbol to create a list of fields that I need to access.
So "TEST.DEMO".split('.')
will become ["TEST", "DEMO"]
Not sure if it will work, but I just wanted to show the main idea.