How to print a particular value from dweet?

285 views Asked by At

I have an output from dweepy as :

[{'content': {'mouse_x': 271,
   'mouse_y': 285,
   'tilt_x': 0,
   'tilt_y': 0,
   'tilt_z': 82},
  'created': '2018-02-10T06:03:02.680Z',
  'thing': 'my_thing_name'}]

My input was :

dweepy.get_latest_dweet_for('my_thing_name')

Question :

How to print value of mouse_x value alone from above dweet output ?

What i tried was :

dweepy.dweet_for('my_thing_name', {'': 'mouse_x'})

which gave me output :

{'content': {'': 'mouse_x'},
 'created': '2018-02-10T06:23:20.320Z',
 'thing': 'my_thing_name',
 'transaction': '6f295639-a667-48ff-bbbf-6dda111333d1'}

How can i print value 271 for mouse_x ?

1

There are 1 answers

1
Sudheesh Singanamalla On BEST ANSWER

For the content you are dweeting with the name as my_thing_name, let us assume that you have dweet_for() as follows initially

dweepy.dweet_for('my_thing_name', {'mouse_x': 271, 'mouse_y': 285, 'tilt_x': 0, 'tilt_y': 0, 'tilt_z': 82})

Which creates a dweet with name as my_thing_name. Now when you query for the dweet using dweepy.get_latest_dweet_for() you are returned a dictionary as follows

a = dweepy.get_latest_dweet_for('my_thing_name')

Result is a list of object whose keys are content, thing and created. For retrieving the content for key mouse_x you need to access the object inside the dweet response's content dictionary.

[{u'content': {u'mouse_y': 285, u'mouse_x': 271, u'tilt_z': 82, u'tilt_x': 0, u'tilt_y': 0}, u'thing': u'my_thing_name', u'created': u'2018-02-10T06:39:53.715Z'}]

This can be done by doing a[0]['content']['mouse_x'] which will return the value 271. This will however work only with the first dweet object. If there are multiple objects returned you can loop through the items and access the mouse_x value by using a[i]['content']['mouse_x'] where i corresponds to the index of the items.

>>> a[0]['content']['mouse_x']
271