0

In sqlalchemy I have a base class Stock.
Now I'd like to bind some previously scraped items and want to bind them like so:

stock = Stock(isin = item['isin'], name = item['name'], points = item['points'], ...)

This feels very unpythonic and boring.
The keys from item are named exactly like the internal representation of Stock, however some items need to be ignored (these start with an underscore, e.g. item['_leave_me_alone']).
Is there a way to write the above in a better way?

Jan
  • 42,290
  • 8
  • 54
  • 79

1 Answers1

2

You could adapt the method used here, or in other words unpack a filtered dict as arguments:

stock = Stock(**{k: v for k, v in item.items() if not k.startswith('_')})
Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127