I'm trying to refactor some code to be DRY
I have an importer that imports from an API that has keys that are different than the destination.
Working code looks like this:
this_event = AnEvent()
wp_event = Requests.get('someurl').json()
if 'title' in wp_event.keys():
this_event.name = wp_event['title']
if 'description' in wp_event.keys():
this_event.description = wp_event['description']
... and so on
What I am trying to do is this:
scrape_fields = [
{
'wp' : 'description',
'django' : 'description'
},
{
'wp' : 'title',
'django' : 'name'
}
... etc
]
for field in scrape_fields:
if field['wp'] in wp_event.keys():
# I knew this would not work when I wrote it but not sure what to do
this_event[field['django']] = wp_event[field['wp']]
But Django throws an exception:
'AnEvent' object does not support item assignment
Is anyone able to advise on how to assign to a field dynamically like this?
'wp's or'django's values? If not we can boost performance of this.