简体   繁体   中英

Passing named tuple field name for a ._replace as an argument from a function

So I have a named tuple, say:

from collections import namedtuple

Symbol = namedtuple('Symbol', 'name code industry date_au open high low close volume weekday_au date_utc_unixtimestamp prev_volume', defaults = ('DEFAULT', 'DEF', 'DEFAULT', '01/01/1970', 0.0, 0.0, 0.0, 0.0, 0, 'Mon', 0, 0))

I make some test data:

ont_data = [
Symbol(name = '1300 SMILES LIMITED', code ='ONT', industry = 'Tech', date_au = '03/05/2020', open = 10.0, high = 12.0, low = 9.5, close = 11.0, volume = 1000, weekday_au = 'Sun', date_utc_unixtimestamp = 123),
Symbol(name = '1300 SMILES LIMITED', code ='ONT', industry = 'Tech', date_au = '15/05/2020', open = 12.0, high = 12.0, low = 5.5, close = 9.0, volume = 999, weekday_au = 'Fri', date_utc_unixtimestamp = 125),
Symbol(name = '1300 SMILES LIMITED', code ='ONT', industry = 'Tech', date_au = '17/01/2020', open = 4.0, high = 90.0, low = 54.5, close = 74.0, volume = 27, weekday_au = 'Wed', date_utc_unixtimestamp = 5)
]

And now I want to create a copy of this named tuple and do some replacements dynamically on a particular attribute:

def set_prev_val(symbol, prev_values, field_to_update):
    """
    Iterate over a copy of Symbol named tuple and replace some attribute values.

    Return a copy of Symbol named tuple with replaced previous values of some field_to_update

    args:
      @symbol - A descending sorted named tuple representing stock symbol data
      @prev_values - A list of previous values (from a descending sorted Symbol)
      @field_to_update - A field in a Symbol named tuple to update
   """
    symbol_copy = symbol

    for row in range(0, len(symbol_copy)):
        symbol_copy[row] = symbol[row]._replace(field_to_update = prev_values[row])
    return symbol_copy

When I test this with say prev_values_data = [1,2,3] I get:

ValueError: Got unexpected field names: ['field_to_update']

Where as what I want is the prev_volume fields to be updated with the values of prev_values_data

I think instead of passing the string value of field_to_update it is taking the actual name of the attribute in the named tuple as the name of the argument!!!

Is there a way for me to be able to pass some 'name' attribute to replace as an argument from a function to a ._replace call in a named tuple? (assuming the name exists in the named tuple attribute names)

Python 3.7

Create an anonymous dict and unpack it:

symbol_copy[row] = symbol[row]._replace(**{field_to_update: prev_values[row]})

Normal keyword argument passing via field_to_update=prev_values[row] is static; it's passing the left-hand side as a fixed string, not a variable (it doesn't even allow quoting it). But dict literal syntax would require quoting for a string key, so it treats field_to_update as a variable, as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM