<s> """<STR_LIT>""" <EOL> import itertools <EOL> import transitfeed <EOL> class MareyGraph : <EOL> """<STR_LIT>""" <EOL> _MAX_ZOOM = <NUM_LIT> <EOL> _DUMMY_SEPARATOR = <NUM_LIT:10> <EOL> def __init__ ( self ) : <EOL> self . _cache = str ( ) <EOL> self . _stoplist = [ ] <EOL> self . _tlist = [ ] <EOL> self . _stations = [ ] <EOL> self . _decorators = [ ] <EOL> self . _tspan = <NUM_LIT:30> <EOL> self . _offset = <NUM_LIT:0> <EOL> self . _hour_grid = <NUM_LIT> <EOL> self . _min_grid = <NUM_LIT:5> <EOL> self . _zoomfactor = <NUM_LIT> <EOL> self . _xoffset = <NUM_LIT:0> <EOL> self . _yoffset = <NUM_LIT:0> <EOL> self . _bgcolor = "<STR_LIT>" <EOL> self . _gwidth = self . _tspan * self . _hour_grid <EOL> def Draw ( self , stoplist = None , triplist = None , height = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> output = str ( ) <EOL> if not triplist : <EOL> triplist = [ ] <EOL> if not stoplist : <EOL> stoplist = [ ] <EOL> if not self . _cache or triplist or stoplist : <EOL> self . _gheight = height <EOL> self . _tlist = triplist <EOL> self . _slist = stoplist <EOL> self . _decorators = [ ] <EOL> self . _stations = self . _BuildStations ( stoplist ) <EOL> self . _cache = "<STR_LIT>" % ( self . _DrawBox ( ) , <EOL> self . _DrawHours ( ) , <EOL> self . _DrawStations ( ) , <EOL> self . _DrawTrips ( triplist ) ) <EOL> output = "<STR_LIT>" % ( self . _DrawHeader ( ) , <EOL> self . _cache , <EOL> self . _DrawDecorators ( ) , <EOL> self . _DrawFooter ( ) ) <EOL> return output <EOL> def _DrawHeader ( self ) : <EOL> svg_header = """<STR_LIT>""" % ( self . _gwidth + self . _xoffset + <NUM_LIT:20> , self . _gheight + <NUM_LIT:15> , <EOL> self . _offset , self . _gheight + <NUM_LIT:10> , <EOL> self . _xoffset , self . _yoffset , self . _zoomfactor ) <EOL> return svg_header <EOL> def _DrawFooter ( self ) : <EOL> return "<STR_LIT>" <EOL> def _DrawDecorators ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT:U+0020>" . join ( self . _decorators ) <EOL> def _DrawBox ( self ) : <EOL> tmpstr = """<STR_LIT>""" % ( <NUM_LIT:0> , <NUM_LIT:0> , self . _gwidth + <NUM_LIT:20> , self . _gheight , self . _bgcolor ) <EOL> return tmpstr <EOL> def _BuildStations ( self , stoplist ) : <EOL> """<STR_LIT>""" <EOL> stations = [ ] <EOL> dists = self . _EuclidianDistances ( stoplist ) <EOL> stations = self . _CalculateYLines ( dists ) <EOL> return stations <EOL> def _EuclidianDistances ( self , slist ) : <EOL> """<STR_LIT>""" <EOL> e_dists2 = [ transitfeed . ApproximateDistanceBetweenStops ( stop , tail ) for <EOL> ( stop , tail ) in itertools . izip ( slist , slist [ <NUM_LIT:1> : ] ) ] <EOL> return e_dists2 <EOL> def _CalculateYLines ( self , dists ) : <EOL> """<STR_LIT>""" <EOL> tot_dist = sum ( dists ) <EOL> if tot_dist > <NUM_LIT:0> : <EOL> pixel_dist = [ float ( d * ( self . _gheight - <NUM_LIT:20> ) ) / tot_dist for d in dists ] <EOL> pixel_grid = [ <NUM_LIT:0> ] + [ int ( pd + sum ( pixel_dist [ <NUM_LIT:0> : i ] ) ) for i , pd in <EOL> enumerate ( pixel_dist ) ] <EOL> else : <EOL> pixel_grid = [ ] <EOL> return pixel_grid <EOL> def _TravelTimes ( self , triplist , index = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> def DistanceInTravelTime ( dep_secs , arr_secs ) : <EOL> t_dist = arr_secs - dep_secs <EOL> if t_dist < <NUM_LIT:0> : <EOL> t_dist = self . _DUMMY_SEPARATOR <EOL> return t_dist <EOL> if not triplist : <EOL> return [ ] <EOL> if <NUM_LIT:0> < index < len ( triplist ) : <EOL> trip = triplist [ index ] <EOL> else : <EOL> trip = triplist [ <NUM_LIT:0> ] <EOL> t_dists2 = [ DistanceInTravelTime ( stop [ <NUM_LIT:3> ] , tail [ <NUM_LIT:2> ] ) for ( stop , tail ) <EOL> in itertools . izip ( trip . GetTimeStops ( ) , trip . GetTimeStops ( ) [ <NUM_LIT:1> : ] ) ] <EOL> return t_dists2 <EOL> def _AddWarning ( self , str ) : <EOL> print str <EOL> def _DrawTrips ( self , triplist , colpar = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> stations = [ ] <EOL> if not self . _stations and triplist : <EOL> self . _stations = self . _CalculateYLines ( self . _TravelTimes ( triplist ) ) <EOL> if not self . _stations : <EOL> self . _AddWarning ( "<STR_LIT>" ) <EOL> self . _stations = self . _CalculateYLines ( self . _Uniform ( triplist ) ) <EOL> if not self . _stations : <EOL> self . _AddWarning ( "<STR_LIT>" ) <EOL> return <EOL> stations = self . _stations <EOL> tmpstrs = [ ] <EOL> servlist = [ ] <EOL> for t in triplist : <EOL> if not colpar : <EOL> if t . service_id not in servlist : <EOL> servlist . append ( t . service_id ) <EOL> shade = int ( servlist . index ( t . service_id ) * ( <NUM_LIT:200> / len ( servlist ) ) + <NUM_LIT> ) <EOL> color = "<STR_LIT>" % hex ( shade ) [ <NUM_LIT:2> : <NUM_LIT:4> ] <EOL> else : <EOL> color = colpar <EOL> start_offsets = [ <NUM_LIT:0> ] <EOL> first_stop = t . GetTimeStops ( ) [ <NUM_LIT:0> ] <EOL> for j , freq_offset in enumerate ( start_offsets ) : <EOL> if j > <NUM_LIT:0> and not colpar : <EOL> color = "<STR_LIT>" <EOL> scriptcall = '<STR_LIT>' % ( t . trip_id , <EOL> t . trip_id , transitfeed . FormatSecondsSinceMidnight ( t . GetStartTime ( ) ) ) <EOL> tmpstrhead = '<STR_LIT>' % ( str ( t . trip_id ) , color , scriptcall ) <EOL> tmpstrs . append ( tmpstrhead ) <EOL> for i , s in enumerate ( t . GetTimeStops ( ) ) : <EOL> arr_t = s [ <NUM_LIT:0> ] <EOL> dep_t = s [ <NUM_LIT:1> ] <EOL> if arr_t is None or dep_t is None : <EOL> continue <EOL> arr_x = int ( arr_t / <NUM_LIT> * self . _hour_grid ) - self . _hour_grid * self . _offset <EOL> dep_x = int ( dep_t / <NUM_LIT> * self . _hour_grid ) - self . _hour_grid * self . _offset <EOL> tmpstrs . append ( "<STR_LIT>" % ( int ( arr_x + <NUM_LIT:20> ) , int ( stations [ i ] + <NUM_LIT:20> ) ) ) <EOL> tmpstrs . append ( "<STR_LIT>" % ( int ( dep_x + <NUM_LIT:20> ) , int ( stations [ i ] + <NUM_LIT:20> ) ) ) <EOL> tmpstrs . append ( '<STR_LIT>' ) <EOL> return "<STR_LIT>" . join ( tmpstrs ) <EOL> def _Uniform ( self , triplist ) : <EOL> """<STR_LIT>""" <EOL> longest = max ( [ len ( t . GetTimeStops ( ) ) for t in triplist ] ) <EOL> return [ <NUM_LIT:100> ] * longest <EOL> def _DrawStations ( self , color = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> stations = self . _stations <EOL> tmpstrs = [ ] <EOL> for y in stations : <EOL> tmpstrs . append ( '<STR_LIT>' % ( color , <NUM_LIT:20> , <NUM_LIT:20> + y + <NUM_LIT> , self . _gwidth + <NUM_LIT:20> , <NUM_LIT:20> + y + <NUM_LIT> ) ) <EOL> return "<STR_LIT>" . join ( tmpstrs ) <EOL> def _DrawHours ( self ) : <EOL> """<STR_LIT>""" <EOL> tmpstrs = [ ] <EOL> for i in range ( <NUM_LIT:0> , self . _gwidth , self . _min_grid ) : <EOL> if i % self . _hour_grid == <NUM_LIT:0> : <EOL> tmpstrs . append ( '<STR_LIT>' % ( i + <NUM_LIT> + <NUM_LIT:20> , <NUM_LIT:20> , i + <NUM_LIT> + <NUM_LIT:20> , self . _gheight ) ) <EOL> tmpstrs . append ( '<STR_LIT>' <EOL> % ( i + <NUM_LIT:20> , <NUM_LIT:20> , <EOL> ( i / self . _hour_grid + self . _offset ) % <NUM_LIT> ) ) <EOL> else : <EOL> tmpstrs . append ( '<STR_LIT>' % ( i + <NUM_LIT> + <NUM_LIT:20> , <NUM_LIT:20> , i + <NUM_LIT> + <NUM_LIT:20> , self . _gheight ) ) <EOL> return "<STR_LIT>" . join ( tmpstrs ) <EOL> def AddStationDecoration ( self , index , color = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> tmpstr = str ( ) <EOL> num_stations = len ( self . _stations ) <EOL> ind = int ( index ) <EOL> if self . _stations : <EOL> if <NUM_LIT:0> < ind < num_stations : <EOL> y = self . _stations [ ind ] <EOL> tmpstr = '<STR_LIT>' % ( color , <NUM_LIT:20> , <NUM_LIT:20> + y + <NUM_LIT> , self . _gwidth + <NUM_LIT:20> , <NUM_LIT:20> + y + <NUM_LIT> ) <EOL> self . _decorators . append ( tmpstr ) <EOL> def AddTripDecoration ( self , triplist , color = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> tmpstr = self . _DrawTrips ( triplist , color ) <EOL> self . _decorators . append ( tmpstr ) <EOL> def ChangeScaleFactor ( self , newfactor ) : <EOL> """<STR_LIT>""" <EOL> if float ( newfactor ) > <NUM_LIT:0> and float ( newfactor ) < self . _MAX_ZOOM : <EOL> self . _zoomfactor = newfactor <EOL> def ScaleLarger ( self ) : <EOL> """<STR_LIT>""" <EOL> newfactor = self . _zoomfactor + <NUM_LIT:0.1> <EOL> if float ( newfactor ) > <NUM_LIT:0> and float ( newfactor ) < self . _MAX_ZOOM : <EOL> self . _zoomfactor = newfactor <EOL> def ScaleSmaller ( self ) : <EOL> """<STR_LIT>""" <EOL> newfactor = self . _zoomfactor - <NUM_LIT:0.1> <EOL> if float ( newfactor ) > <NUM_LIT:0> and float ( newfactor ) < self . _MAX_ZOOM : <EOL> self . _zoomfactor = newfactor <EOL> def ClearDecorators ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _decorators = [ ] <EOL> def AddTextStripDecoration ( self , txtstr ) : <EOL> tmpstr = '<STR_LIT>' % ( <NUM_LIT:0> , <EOL> <NUM_LIT:20> + self . _gheight , txtstr ) <EOL> self . _decorators . append ( tmpstr ) <EOL> def SetSpan ( self , first_arr , last_arr , mint = <NUM_LIT:5> , maxt = <NUM_LIT:30> ) : <EOL> s_hour = ( first_arr / <NUM_LIT> ) - <NUM_LIT:1> <EOL> e_hour = ( last_arr / <NUM_LIT> ) + <NUM_LIT:1> <EOL> self . _offset = max ( min ( s_hour , <NUM_LIT> ) , <NUM_LIT:0> ) <EOL> self . _tspan = max ( min ( e_hour - s_hour , maxt ) , mint ) <EOL> self . _gwidth = self . _tspan * self . _hour_grid </s>
