Re: How to convert a train running program from synchronous to asynchronous?

2019-04-27 Thread Irv Kalb

> On Apr 26, 2019, at 4:18 AM, Arup Rakshit  wrote:
> 
> I have modelled which starts running once drivers and stations are assigned 
> to it. Otherwise, it doesn’t run, really don’t care if passengers are boarded 
> or not at this moment. :) I think this program can help me to introduce to 
> the Python async programming domain.
> 
> Here is my program:
> 
> # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
> # train.py
> # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
> 
> import time
> import random
> 
> from user import User
> 
> class NotReadyToDeparture(Exception):
>pass
> 
> class Train:
>def __init__(self, name, category):
>self.name = name
>self.category = category
>self.__drivers = []
>self.__stations = []
> 
>@property
>def drivers(self):
>return self.__drivers
> 
>@drivers.setter
>def drivers(self, coachmen):
>self.__drivers = coachmen
> 
>@property
>def stations(self):
>return self.__stations
> 
>@stations.setter
>def stations(self, places):
>self.__stations = places
> 
>def next_stoppage(self):
>return self.stations[0]
> 
>def run(self):
>total_run_time = 0
>if len(self.drivers) == 0:
>raise NotReadyToDeparture('Drivers are not yet boarded')
> 
>if len(self.stations) == 0:
>raise NotReadyToDeparture('Stoppage stations are not yet 
> scheduled')
> 
>for station in range(len(self.stations[:])):
>run_time_to_next_stoppage = random.randint(2, 6)
>time.sleep(run_time_to_next_stoppage)
>total_run_time += run_time_to_next_stoppage
>print("Train Reached at {0} in {1} 
> seconds".format(self.stations.pop(0), run_time_to_next_stoppage))
> 
>print(f"Train {self.name} is reached the destination in 
> {total_run_time} seconds.")
> 


The underlying problem with your current "run" method is that it only returns 
when the train has visited all the stations.  This happens because of your for 
loop and the call to time.sleep inside your loop.   

Instead, I would suggest that you split the functionality of your current run 
method into two methods. The "run" method would start the train going.  Then 
you need a separate method, maybe "update" that would be called continuously.  
(Code below is completely untested)

The run method would consist of your current checks to ensure that the drivers 
and stations are set.  Then, it should set:

 self.total_run_time = 0 # making this an instance variable that will 
be used in the update method

You also need a way of getting the current (real) time so you can know when you 
have reached the next station (I'll leave the implementation of that up to you):

  self.stations.insert(0, 'start')   # add something at the beginning of 
your list that will get popped off immediate at the first call to update
  self.time_at_next_station =  


The update method will be called continuously.  It should check the current 
time and see if it has reached  the time to move on to the next station, 
something like:

def update(): 
 currentTime =   
  if currentTime < self.time_at_next_station:  # in transit to the next 
station, nothing to do
return

   # Train has arrived at a station, set up for next station
   self.stations.pop(0)  # eliminate the station at the front of the 
list
   if len(self.stations) == 0:
return  # reached the end of the line

run_time_to_next_stoppage = random.randint(2, 6) # whatever time this 
happened plus some random seconds
self.time_at_nextStation = currentTime + run_time_to_next_stoppage 
# time in the future when train gets to next station
self.total_run_time = self.total_run_time + run_time_to_next_stoppage  
# add to total time
print('Next stoppage is at', stations[0])
  

The first time "update" is called, it will move from the 'start' to the first 
station, and calculate when the train should reach the first station.  Because 
update is called continuously, it only needs to check if the time for getting 
to the next station has been reached.  If so, then set up for the next station.

That way, your main loop becomes:

train.run()
while len(train.stations) is not 0: 
 train.update()

Then you could have another property to get the total time (and print).

Personally, I would prefer that train.update would return True or False to say 
if it was done with the whole route.  that is, it would normally return False 
to say it is not done yet.  But when it gets to the end, it would return True.  
Then your main code could be written as:

train.run()
while True:
done = train.update()
if done:
 break

Like I 

2019 John Hunter Excellence in Plotting Contest Reminder

2019-04-27 Thread Nelle Varoquaux
Hi everybody,


My apologies to those of you getting this on multiple lists.


In memory of John Hunter, we are pleased to be announce the SciPy John
Hunter Excellence in Plotting Competition for 2019. This open competition
aims to highlight the importance of data visualization to scientific
progress and showcase the capabilities of open source software.

Participants are invited to submit scientific plots to be judged by a
panel. The winning entries will be announced and displayed at the
conference.

John Hunter’s family and NumFocus are graciously sponsoring cash prizes for
the winners in the following amounts:


   -

   1st prize: $1000
   -

   2nd prize: $750
   -

   3rd prize: $500



   -

   Entries must be submitted by June, 8th to the form at
   https://goo.gl/forms/cFTB3FUBrMPfQ7Vz1
   -

   Winners will be announced at Scipy 2019 in Austin, TX.
   -

   Participants do not need to attend the Scipy conference.
   -

   Entries may take the definition of “visualization” rather broadly.
   Entries may be, for example, a traditional printed plot, an interactive
   visualization for the web, or an animation.
   -

   Source code for the plot must be provided, in the form of Python code
   and/or a Jupyter notebook, along with a rendering of the plot in a widely
   used format.  This may be, for example, PDF for print, standalone HTML and
   Javascript for an interactive plot, or MPEG-4 for a video. If the original
   data can not be shared for reasons of size or licensing, "fake" data may be
   substituted, along with an image of the plot using real data.
   -

   Each entry must include a 300-500 word abstract describing the plot and
   its importance for a general scientific audience.
   -

   Entries will be judged on their clarity, innovation and aesthetics, but
   most importantly for their effectiveness in communicating a real-world
   problem. Entrants are encouraged to submit plots that were used during the
   course of research or work, rather than merely being hypothetical.
   -

   SciPy reserves the right to display any and all entries, whether
   prize-winning or not, at the conference, use in any materials or on its
   website, with attribution to the original author(s).


SciPy John Hunter Excellence in Plotting Competition Co-Chairs

Hannah Aizenman

Thomas Caswell

Madicken Munk

Nelle Varoquaux
-- 
https://mail.python.org/mailman/listinfo/python-list