Here I am pulling json data from different websites and I intend to embed some of the retrived information into instance variables. The trouble is, the json package retrieved keeps the info I want under different keys and list positions, and so each dictionary address is unique per website.
I am struggling to find a way of creating a new instance of a class and passing different dictionary keys to lookup for when new data is retrived.
Something like this would be too easy I feel...
import requests, json
class B(object):
def __init__(self, name, url, size, price):
self.name = name
self.url = url
self.size_address = size
self.price_address = price
self.size = 0
self.price = 0
self.data = {}
def retrieve(self):
#Data grab from web
try:
grab = requests.get(self.url, timeout=10)
self.data = tick.json()
except:
raise RuntimeError(self.name + 'Error')
def size(self):
self.size = data[self.size_address]
print self.size
def price(self):
self.price = data[self.price_address]
print self.price
>>> a = B('Dave','www.dave.com/api',['names'][0]['size'],[['names'][0]['prices'][0])
>>> a.size()
42030.20
I've had a look at abstract methods as well as binding functions written outside of the class definition. Binding functions looked to be promising but I couldn't create the same function for each class with different variables because I would be using the same name.
Hopefully someone can point me in the right direction.