0

I need help with function attribute in Python. I have the data which look like this: exch1 = data['Info']['Exchange1'] and this equals for example poloniex. I need to get exch1_object which must look like this ccxt.poloniex(), But when I try to do it I get the error:

AttributeError: module 'ccxt' has no attribute 'str'
import ccxt
exch1 = data['Info']['Exchange1']
exch2 = data['Info']['Exchange2']
exch1_object = exch1.lower()
exch1_object = ccxt.str(exch1_object)()
gosuto
  • 5,422
  • 6
  • 36
  • 57
Arrmlet
  • 69
  • 2
  • 6

1 Answers1

0

You could try to use getattr():

# 'exch1_object' should be a string
func = getattr(ccxt, exch1_object)
exch1_object = func()

This will get the function from the module ccxt that has the name that is currently inside the variable exch1_object (which should be a string).

This can also be shortened to this:

exch1_object = getattr(ccxt, exch1_object)()

you will have to decide which is clearer and more readable for your case.


So, for example, these lines:

exch1_object = 'poloniex'
exch1_object = getattr(ccxt, exch1_object)()

will yield the same results as:

exch1_object = ccxt.poloniex()
Ralf
  • 16,086
  • 4
  • 44
  • 68