Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can this be done?
#1
I have been using a Tingbot which runs on a modified version of Jessie.  There is an interesting feature (called a decorator) in the Tingbot Python that would be nice to have in Python in general, so I am wondering if there is anything similar to this feature?  (From the Tingbot Python documentation):

@every(hours=0, minutes=0, seconds=0)
This decorator will call the function marked periodically, according to the time specified.

Example: Refreshing data every 10 minutes

@every(minutes=10)
def refresh_data():
  r = requests.get('ht**://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=bd82977b86bf27fb59a04b61b657fb6f')
  state['data'] = r.json()
Reply
#2
decorators are available in "standard" python.

https://www.python.org/dev/peps/pep-0318/

google it for additional resources or tutorials
Reply
#3
https://docs.python.org/3/library/thread...er-objects
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
Buran- Thanks, I did Google it but the PEP document never came up in the results... I don't want to *build* the decorator from an unfinished spec, I would just like to see it implemented.

Wavic- Thanks, but the timer() only works once.
In this test code below, I would expect to see the timeout message every fifth tick, but the timeout() function only happens once. The Tingbot decorator runs forever. (Or, am I not using the timer function correctly??)

#!/usr/bin/env python
#!/usr/bin/env/python3

from threading import Timer
import time

def timeout():
print("5-seconds elapsed")

t = Timer(5, timeout)
t.start()

# do something else
x=1
while x==1:
print "tick"
time.sleep(1)
Reply
#5
Please read this link on how to post code: Help BBCode or click the "BBCode Help" button at the bottom of the edit window.

As to your last post, why would you expect it to trigger more than once when you are only triggering it once.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020