farhanaktar
Badges: 7
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#1
Report Thread starter 2 years ago
#1
Can someone explain me how can i start doing python coding for the task below.
0
reply
farhanaktar
Badges: 7
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#2
Report Thread starter 2 years ago
#2
This is the question below. Can someone explain it to me please or at least give me hint on start it off.
Attached files
0
reply
winterscoming
Badges: 19
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#3
Report 2 years ago
#3
There's some good examples of using python's sched module here: https://pymotw.com/3/sched/ - try pasting some of those into your code editor and run the programs to get a feel for how the scheduler works.

The time library is a bit clunky, but also fairly simple/primitive as well - it works using the concept of 'seconds elapsed since the UNIX Epoch (01-01-1970)
https://docs.python.org/3/library/time.html or https://pymotw.com/3/time/

The time library is really useful for working with sched. But if you want to display the time to the user, or convert a user-entered time into seconds-since-epoch, then datetime might be useful: https://pymotw.com/3/datetime/


In case it's not clear how the sched module works, its 'enter' and 'enterabs' functions are higher-order functions. When you use the name of another function in the arguments for enter or enterabs, that other function is added into the scheduler's internal data structure (priority queue), along with some kind of time/delay which tells the scheduler when to call that other function (a 'trigger' ).

By adding the other function and the time/priority into the scheduler, this effectively creates an event where an event is just a trigger which calls a function (sometimes the function to be called is known as an 'event handler' since it's a function which 'handles the event' - i.e. gets called when the event is triggered). In this scenario, the trigger/event is just a timestamp. i.e. The scheduler has it's own internal while-loop which is repeatedly checking time every time it cycles. It'll keep repeating as long as there are not-yet-happened events still to run. It's able to keep checking the current time and continue repeating, triggering those events (calling their functions) at the right time, until all the scheduler's events have been triggered.


So, keeping in mind that the scheduler is basically just a Loop and a Priority Queue, that means you can add events to the scheduler at any point (Which is quite useful if you want to be able to repeat events -- the scheduler is already doing repetition so you don't need to. i.e. if an event re-adds itself to the queue, then it will be repeated, or an event could add a different event, etc).itself back into the scheduler. )


Just to convey how simple the scheduler really is, the pseudocode for scheduler.run() would do something similar to this:
Code:
WHILE Priority Queue IS NOT Empty

  GET events from Priority Queue whose Trigger timestamp in seconds is equal to Current Time in seconds

  IF events exist THEN
      FOR EACH event IN events (ORDER BY Priority)
          CALL event function with its arguments
          REMOVE event from Priority Queue
      END FOR

      SLEEP for 1 second
  END IF

END WHILE
(So you can see that the place where the scheduler will call an event function is within a loop, and that function will be called once for each time it has been entered into the priority queue -- so if you decided to write some code which causes it to be re-added to the queue many times, then the scheduler would just see it as a new event every time.)
Last edited by winterscoming; 2 years ago
1
reply
zulhaq345
Badges: 8
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#4
Report 2 years ago
#4
so is this the code for the alrm system question
0
reply
winterscoming
Badges: 19
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#5
Report 2 years ago
#5
(Original post by zulhaq345)
so is this the code for the alrm system question
No, it's pseudocode which shows approximately how Python's scheduler works and what it's really doing under the hood. It's useful to know how the scheduler works when using it so that you can see what's going on (i.e. the scheduler isn't just a "magic" black box).

There are examples of using the scheduler on the links I posted above - it's worth trying the examples and experimenting to see how they work. The best way to approach a problem is to try to solve small pieces of it at a time, then gradually build it up as you get a better understanding of what you need to do, how everything works, and what the problems are really asking you for.
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Y13's - If you haven't confirmed your firm and insurance choices yet, why is that?

I am waiting until the deadline in case anything in my life changes (18)
21.18%
I am waiting until the deadline in case something else changes (e.g. exams/pandemic related concerns) (11)
12.94%
I am waiting until I can see the unis in person (5)
5.88%
I still have more questions before I make my decision (15)
17.65%
No reason, just haven't entered it yet (17)
20%
Something else (let us know in the thread!) (19)
22.35%

Watched Threads

View All