I am trying to add a new property to a named tuple in which I want to store some values. Post that I want to convert it back to JSON to pass it as a parameter in api. I found an answer to add a property to a namedTuple however it creates a type and doesn't modify the instance itself. What is the best way to achieve above functionality.
how do I add fields to a namedtuple?
def _json_object_hook(d):
return namedtuple('X', d.keys())(*d.values())
def json2obj(data):
return json.loads(data, object_hook=_json_object_hook)
def lambda_handler(event, context):
registrationResponse = s3.get_object(Bucket=bucket, Key=registrationkey)
registrationResponse = registrationResponse['Body'].read().decode('utf-8')
registration = json2obj(registrationResponse)
abc = namedtuple('registration',registration._fields+('point',))
y = abc._asdict() # TypeError: _asdict() missing 1 required positional argument: 'self'
On debugger
type(abc) #<class 'type'>
type(registration) #<class '__main__.X'>
registration #<X, len() = 7>
abc #<class '__main__.registration'>