Using Classes

Mag Gam magawake at gmail.com
Sat Jun 26 08:19:34 EDT 2010


Oh wow. You went beyond :-)

Let me rewrite the example. I only want to calculate the wait time
which is basically the depart time minus the arrival time for multiple
days.

This is all on 1 station.

June 26, 2010:
Trian A, Arrived at 6:00AM, Depart at 9:00AM
Trian B, Arrived at 2:00AM, Depart at 2:30AM
Trian C, Arrived at 4:00AM, Depart at 4:30AM
Trian D, Arrived at 7:00AM, Depart at 9:00AM

June 27, 2010:
Trian A, Arrived at 6:00AM, Depart at 9:15AM
Trian B, Arrived at 2:00AM, Depart at 2:35AM
Trian C, Arrived at 4:00AM, Depart at 4:35AM
Trian D, Arrived at 6:40AM, Depart at 9:03AM


Let say I want to just display wait time (Depart-Arrived):
June 26, 2010
TrianA: 3:00:00
TrianB: 0:30:00
TrianC: 0:30:00
TrianD: 2:00:00

Total=6:00:00

and the same for June 27.

I have already built out the functions and the dictionaries, just
wondering how and where I can do it in OOP.






On Sat, Jun 26, 2010 at 2:39 AM, Dennis Lee Bieber
<wlfraed at ix.netcom.com> wrote:
> On Fri, 25 Jun 2010 19:41:43 -0400, Mag Gam <magawake at gmail.com>
> declaimed the following in gmane.comp.python.general:
>
>> Thanks everyone for your responses. They were very useful and I am
>> glad I asked the question.
>>
>> I think having a concrete example would help me better, lets say I have this.
>>
>>
>> Trian A, Arrived at 6:00AM Jun 25, Left at 8:00AM Jun 25
>> Trian B, Arrived at 2:00AM Jun 26, Left at 12:00AM Jun 26
>> Trian C, Arrived at 4:00AM Jun 2, Left at 11:00AM Jun 2
>> Trian D, Arrived at 7:00AM Jun 11, Left at 3:00AM Jun 11
>>
>        Arrived where? Left where? Especially as "Trian D" "left" four hours
> before it "arrived"... <G>
>
>> Does that look right? Lets say I want to figure out how long each
>> train waited on the platfor
>>
>        I'd probably model that as an attribute of a station/platform
>
>        This doesn't quite answer your specific "how long" question (in
> truth, I've got the reverse here -- each Station has a fixed "wait"
> time, but the time is different for different stations).
>
>        It also isn't a very well thought out example, I wrote it on the
> fly. A better simulation would use callbacks into each train, and time
> ordered queue of events. That way the main loop would basically consist
> of pulling off the first (lowest time value) event -- set that time as
> "now", invoke the callback passing "now" (which would activate the
> method in the train that defined that event); the train would then put
> out its status, compute its next event time, insert that new event onto
> the queue (being a time-ordered priority queue, the new event is
> inserted at the proper time place, not just at the end). Instead, I'm
> using a simple incrementing time and invoking each train with "now",
> letting each train compute if the event time has been reached.
>
>
> -=-=-=-=-=-=-=-
> import time
>
> class Station(object):
>    def __init__(self, name, waitTime):
>        #waitTime is in hours
>        self.name = name
>        self.waitTime = waitTime
>
> class Route(object):
>    def __init__(self, name):
>        self.name = name
>        self.stations = []
>    def next(self, current):
>        to = (current + 1) % len(self.stations)
>        return to
>    def addStation(self, station):
>        self.stations.append(station)
>
> class Train(object):
>    def __init__(self, name, speed, route, currentStation):
>        #speed is in distance-units per hour
>        self.name = name
>        self.speed = speed
>        self.route = route
>        self.station = currentStation
>        self.nextEventTime = 0
>        self.enroute = False
>    def update(self, time):
>        if (self.enroute
>            and (time >= self.nextEventTime)):
>            self.enroute = False
>            self.nextEventTime = (time
>                                  + self.route.stations[
>                                      self.station].waitTime)
>            print ("%8s\t%-15s\tTrain %s arriving %s"
>                   % (time,
>                      self.route.name,
>                      self.name,
>                      self.route.stations[self.station].name))
>        elif (not self.enroute
>              and (time >= self.nextEventTime)):
>            self.enroute = True
>            currentStation = self.station
>            self.station = self.route.next(currentStation)
>            self.nextEventTime = (time
>                                  + (globalMap[
>  (self.route.stations[currentStation],
>  self.route.stations[self.station]) ]
>                                     / self.speed))
>            print ("%8s\t%-15s\tTrain %s departing %s for %s"
>                   % (time,
>                      self.route.name,
>                      self.name,
>                      self.route.stations[currentStation].name,
>                      self.route.stations[self.station].name))
>        else:
>            pass
>
> class Railroad(object):
>    def __init__(self, trains, time=0):
>        self.trains = trains
>        self.time = time
>        for train in self.trains:
>            train.time = self.time
>    def update(self, timeDelta=0.125):
>        #timeDelta is in hours.
>        self.time += timeDelta
>        for train in self.trains:
>            train.update(self.time)
>
> ############
> # create a few stations
>
> london = Station("London", 0.5)
> paris = Station("Paris", 0.33)
> moscow = Station("Moscow", 1.51)
> athens = Station("Athens", 0.5)
> rome = Station("Rome", 0.25)
> madrid = Station("Madrid", 0.25)
> beijing = Station("Beijing", 0.2)
> mumbai = Station("Mumbai", 0.45)
> berlin = Station("Berlin", 0.33)
> edinburgh = Station("Edinburgh", 0.33)
>
> # define world map (distance table); highly fictional data
> globalMap = {   (london, paris) : 500,      (london, moscow) : 1250,
>                (london, athens) : 1000,    (london, rome) : 750,
>                (london, madrid) : 750,     (london, beijing) : 2500,
>                (london, mumbai) : 2000,    (london, berlin) : 675,
>                (london, edinburgh) : 450,
>                (paris, moscow) : 750,      (paris, athens) : 500,
>                (paris, rome) : 250,        (paris, madrid): 250,
>                (paris, beijing) : 2000,    (paris, mumbai) : 1500,
>                (paris, berlin) : 175,      (paris, edinburgh) : 950,
>                (moscow, athens) : 750,     (moscow, rome) : 1000,
>                (moscow, madrid) : 1500,    (moscow, beijing) : 1250,
>                (moscow, mumbai) : 750,     (moscow, berlin) : 750,
>                (moscow, edinburgh) : 1700,
>                (athens, rome) : 750,       (athens, madrid) : 1000,
>                (athens, beijing) : 2000,   (athens, mumbai) : 1000,
>                (athens, berlin) : 325,     (athens, edinburgh) : 1450,
>                (rome, madrid) : 750,       (rome, beijing) : 2250,
>                (rome, mumbai) : 1750,      (rome, berlin) : 500,
>                (rome, edinburgh) : 1200,
>                (madrid, beijing) : 2500,   (madrid, mumbai) : 1750,
>                (madrid, berlin) : 500,     (madrid, edinburgh) : 1200,
>                (beijing, mumbai) : 800,    (beijing, berlin) : 2250,
>                (beijing, edinburgh) : 2950,
>                (mumbai, berlin) : 1250,    (mumbai, edinburgh) : 2450,
>                (berlin, edinburgh) : 1125  }
>
> # create reverse mappings
> for (frm, to) in globalMap.keys():
>    globalMap[ (to, frm) ] = globalMap[ (frm, to) ]
>
> # create some routes; remember routes are cycles, the last city connects
> to
> # the first
> orient = Route("Orient Express")
> worldTour = Route("WorldTour")
> euroZone = Route("Europe")
>
> orient.addStation(paris)
> orient.addStation(athens)
> orient.addStation(moscow)
> orient.addStation(mumbai)
> orient.addStation(beijing)
> orient.addStation(moscow)
>
> worldTour.addStation(edinburgh)
> worldTour.addStation(london)
> worldTour.addStation(madrid)
> worldTour.addStation(rome)
> worldTour.addStation(athens)
> worldTour.addStation(mumbai)
> worldTour.addStation(beijing)
> worldTour.addStation(moscow)
> worldTour.addStation(berlin)
> worldTour.addStation(paris)
>
> euroZone.addStation(paris)
> euroZone.addStation(madrid)
> euroZone.addStation(rome)
> euroZone.addStation(athens)
> euroZone.addStation(berlin)
>
> #create trains
> wt = Train("World Tour", 75, worldTour, 0)
> oe = Train("Orient Express", 125, orient, 0)
> exp = Train("Morning Express", 150, euroZone, 0)
> mc = Train("Morning Commute", 50, euroZone, 0)
> ec = Train("Evening Commute", 50, euroZone, 3)
>
> #create railroad
> rr = Railroad([wt, oe, exp, mc, ec])
>
> # start running
> while True:
>    rr.update()
>    time.sleep(0.25)
>
> -=-=-=-=-=-=-=-
>
> Definitely not realistic... My "Orient Express" (Paris to Beijing and
> back, passing through Moscow both ways) takes 42 "hours" for the
> round-trip.
>
>    0.125       WorldTour       Train World Tour departing Edinburgh for
> London
>   0.125        Orient Express  Train Orient Express departing Paris for
> Athens
>   0.125        Europe          Train Morning Express departing Paris for
> Madrid
>   0.125        Europe          Train Morning Commute departing Paris for
> Madrid
>   0.125        Europe          Train Evening Commute departing Athens for
> Berlin
>   1.125        Europe          Train Morning Express arriving Madrid
>   1.375        Europe          Train Morning Express departing Madrid for
> Rome
>   4.125        Orient Express  Train Orient Express arriving Athens
>   4.625        Orient Express  Train Orient Express departing Athens for
> Moscow
>   5.125        Europe          Train Morning Commute arriving Madrid
>   5.375        Europe          Train Morning Commute departing Madrid for
> Rome
>   6.125        WorldTour       Train World Tour arriving London
>   6.125        Europe          Train Evening Commute arriving Berlin
>   6.375        Europe          Train Morning Express arriving Rome
>     6.5        Europe          Train Evening Commute departing Berlin for
> Paris
>   6.625        WorldTour       Train World Tour departing London for Madrid
>   6.625        Europe          Train Morning Express departing Rome for
> Athens
>     9.5        Europe          Train Evening Commute arriving Paris
>   9.875        Europe          Train Evening Commute departing Paris for
> Madrid
>  10.625        Orient Express  Train Orient Express arriving Moscow
>  11.625        Europe          Train Morning Express arriving Athens
>  12.125        Europe          Train Morning Express departing Athens for
> Berlin
>   12.25        Orient Express  Train Orient Express departing Moscow for
> Mumbai
>  14.125        Europe          Train Morning Express arriving Berlin
>    14.5        Europe          Train Morning Express departing Berlin for
> Paris
>  14.875        Europe          Train Evening Commute arriving Madrid
>  15.125        Europe          Train Evening Commute departing Madrid for
> Rome
>    15.5        Europe          Train Morning Express arriving Paris
>  15.875        Europe          Train Morning Express departing Paris for
> Madrid
>  16.625        WorldTour       Train World Tour arriving Madrid
>  16.875        WorldTour       Train World Tour departing Madrid for Rome
>  16.875        Europe          Train Morning Express arriving Madrid
>  17.125        Europe          Train Morning Express departing Madrid for
> Rome
>   18.25        Orient Express  Train Orient Express arriving Mumbai
>   18.75        Orient Express  Train Orient Express departing Mumbai for
> Beijing
>  20.375        Europe          Train Morning Commute arriving Rome
>  20.625        Europe          Train Morning Commute departing Rome for
> Athens
>  22.125        Europe          Train Morning Express arriving Rome
>  22.375        Europe          Train Morning Express departing Rome for
> Athens
> --
>        Wulfraed                 Dennis Lee Bieber         AF6VN
>        wlfraed at ix.netcom.com    HTTP://wlfraed.home.netcom.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list