tingbot / tingbot-python

🔩 Python APIs to write apps for Tingbot
Other
18 stars 9 forks source link

Problems with using `tingbot.app.settings`? #47

Closed omgmog closed 7 years ago

omgmog commented 7 years ago

Tried testing tingbot.app.settings functionality as described in https://tingbot-python.readthedocs.io/en/latest/settings.html

I've got an example app with the following:

import tingbot
from tingbot import *

@every(seconds=1.0/30)
def loop():
    screen.fill(color='black')
    screen.text('Hello %s' % tingbot.app.settings['name'])

@left_button.press
def press():
    tingbot.app.settings["name"] = 'Omgmog'
    tingbot.app.settings.save()

tingbot.run()

I've provided a default_settings.json with the following:

{
    "name": "Max"
}

When the app runs, it correctly prints Hello Max.

When I press the button, I'm presented with:

TypeError
main.py:12

After this, at the next @each tick the text Hello Omgmog is printed to the screen.

In the Tide console I have the following:

<open file '/tmp/tide/7c9313b0-84a6-11e6-af13-877e07079bbe.tingapp/local_settings.json', mode 'w' at 0x7104df40> is not JSON serializable
Traceback (most recent call last):                                                                                              
  File "/usr/local/lib/python2.7/dist-packages/tingbot/run_loop.py", line 79, in run                                            
    self._wait(next_timer.next_fire_time)                                                                                       
  File "/usr/local/lib/python2.7/dist-packages/tingbot/run_loop.py", line 113, in _wait                                         
    self._wait_callbacks()                                                                                                      
  File "/usr/local/lib/python2.7/dist-packages/tingbot/utils.py", line 13, in __call__                                          
    callback()                                                                                                                  
  File "/usr/local/lib/python2.7/dist-packages/tingbot/button.py", line 139, in wait                                            
    button.run_callbacks()                                                                                                      
  File "/usr/local/lib/python2.7/dist-packages/tingbot/button.py", line 66, in run_callbacks                                    
    self.callbacks[action.type]()                                                                                               
  File "/usr/local/lib/python2.7/dist-packages/tingbot/utils.py", line 13, in __call__                                          
    callback()                                                                                                                  
  File "/tmp/tide/7c9313b0-84a6-11e6-af13-877e07079bbe.tingapp/main.py", line 15, in press                                      
    tingbot.app.settings["name"] = "Omgmog"                                                                                     
  File "/usr/local/lib/python2.7/dist-packages/tingbot/tingapp.py", line 72, in __setitem__                                     
    self.save()                                                                                                                 
  File "/usr/local/lib/python2.7/dist-packages/tingbot/tingapp.py", line 92, in save                                            
    save_json(os.path.join(self.path, 'local_settings.json'), self.local_settings)                                              
  File "/usr/local/lib/python2.7/dist-packages/tingbot/tingapp.py", line 25, in save_json                                       
    json.dump(fp, obj)                                                                                                          
  File "/usr/lib/python2.7/json/__init__.py", line 189, in dump                                                                 
    for chunk in iterable:                                                                                                      
  File "/usr/lib/python2.7/json/encoder.py", line 442, in _iterencode                                                           
    o = _default(o)                                                                                                             
  File "/usr/lib/python2.7/json/encoder.py", line 184, in default                                                               
    raise TypeError(repr(o) + " is not JSON serializable")                                                                      
TypeError: <open file '/tmp/tide/7c9313b0-84a6-11e6-af13-877e07079bbe.tingapp/local_settings.json', mode 'w' at 0x7104df40> is not JSON serializable 
joerick commented 7 years ago

Hey @omgmog. Thanks for the report, I've reproduced it here. I'm taking a look now...

joerick commented 7 years ago

Fixed in v1.0.2!

omgmog commented 7 years ago

:100: Cheers!