Python Forum

Full Version: Dictionary value not recognized
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Im fetching data from firebase like this:

#imports....
class AIHome:

def __init__(self, onTime, offTime):
#init stuff

def fetchUpdate(self):
AIHome.authentication = firebase.FirebaseAuthentication('mykey', 'myemail', extra={'id': 123})
AIHome.firebase = firebase.FirebaseApplication('https://myapp.firebaseio.com/', AIHome.authentication)
print AIHome.authentication.extra

AIHome.user = AIHome.authentication.get_user()
print AIHome.user.firebase_auth_token

AIHome.results = AIHome.firebase.get('/Relays', None)#, {'print': 'pretty'})
print AIHome.results
print AIHome.results.Relay1ON #when i try to use it here it fails!!!!!
print AIHome.results.Relay1OFF

self.relayToggle()
it returns this:


{u'Relay1ON': 1800, u'Relay1OFF': u'0600'}
so when I try to use the dict and read its value I get this:

File "tsrb430.py", line 54, in fetchUpdate
    print AIHome.results.Relay1ON
AttributeError: 'dict' object has no attribute 'Relay1ON'
Please help
Indentation is wrong in first code.

You can not do a dot'ed call on a dictionary.
>>> results = {u'Relay1ON': 1800, u'Relay1OFF': u'0600'}
>>> results.Relay1ON
Traceback (most recent call last):
 File "<string>", line 301, in runcode
 File "<interactive input>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'Relay1ON'

>>> results['Relay1ON']
1800
>>> #Or get()
>>> results.get('Relay1ON', 'Not here')
1800
>>> results = {u'Relay1ON999': 1800, u'Relay1OFF': u'0600'}
>>> results.get('Relay1ON', 'Not here')
'Not here'