dimanche 24 novembre 2019

Best way to re-assign arg values in function

I think it would be easiest to explain what I mean by showing code first:

def indicator(
        self, symbol: str = None, timeframe: str = None,
        indicator: str = None, period: int = 0, instances: int = 0,
        *args, **kwargs):
    data = self.ohlcv(symbol, timeframe, instances + period)

    for arg in args:
        if arg == 'open':
            arg = data['open'].to_numpy()
        elif arg == 'high':
            arg = data['high'].to_numpy()
        elif arg == 'low':
            arg = data['low'].to_numpy()
        elif arg == 'close':
            arg = data['close'].to_numpy()
        elif arg == 'volume':
            arg = data['volume'].to_numpy()
        else:
            pass

    values = getattr(ta.func, indicator)(args, kwargs)
    return values

Here I am re-assigning args that are strings (i.e. open) to an array of open values from a dataframe. Is there an easier way,or more pythonic/terse way of doing this than using a bunch of if and elif statements? I feel i could do something like setattr for each column in the dataframe but I would not know how to pass them to the second ta.func function.

Thank you!

Aucun commentaire:

Enregistrer un commentaire