Q:

Dynamic methods based on a dict in python

I am working on creating a python module for getting stocks data.
I have a dictionary:
{'StockSymbol': 'AMD', 'LastTradeTime': '4:00PM EST', 'ChangePercent': '+0.58', 'ID': '327', 'LastTradeDateTimeLong': 'Mar 10, 4:00PM EST', 'Index': 'NASDAQ', 'LastTradeWithCurrency': '13.91', 'LastTradeDateTime': '2017-03-10T16:00:02Z', 'LastTradePrice': '13.91', 'LastTradeSize': '0', 'PreviousClosePrice': '13.33'}

Currently I have 11 methods such as:
class Stock(object):

    def getSymbol():
      return self.data['StockSymbol']

    def getLastTradeTime():
      return self.data['LastTradeTime']

    ........

I use it as:
google = Stock('GOOG')
print(google.getLastTradeTime()) //4:00PM EST

My question is, Is it possible to generate these methods dynamically? 
So I could do google.getLastTradeSize() etc without defining them.
Here is a Python fiddle: https://repl.it/GSG1

A:

In Python there's a design pattern called bunch, it works like this, I believe it can solve your problem:
class Bunch(dict):
    def __init__(self, *args, **kwargs):
        super(Bunch, self).__init__(*args, **kwargs)
        self.__dict__ = self

    def __getattribute__(self, item):
        try:
            return object.__getattribute__(self, item)
        except:
            return None

my_dict = {'StockSymbol': 'AMD', 'LastTradeTime': '4:00PM EST', 'ChangePercent': '+0.58', 'ID': '327',
           'LastTradeDateTimeLong': 'Mar 10, 4:00PM EST', 'Index': 'NASDAQ', 'LastTradeWithCurrency': '13.91',
           'LastTradeDateTime': '2017-03-10T16:00:02Z', 'LastTradePrice': '13.91', 'LastTradeSize': '0',
           'PreviousClosePrice': '13.33'}

obj = Bunch(**my_dict)
print obj.StockSymbol
print obj.LastTradeTime
print obj.key_not_exist

And we get:
AMD

4:00PM EST

None

So you don't have to define your so-called gettter method, like what you do in Java/C++; 
PS: in a real project, you can also inherit from this Bunch class.

