<s> """<STR_LIT>""" <EOL> import collections <EOL> from datetime import date <EOL> from functools import partial <EOL> from itertools import chain <EOL> import inspect <EOL> import json <EOL> import time <EOL> from django . utils . safestring import mark_safe <EOL> __title__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> __author__ = '<STR_LIT>' <EOL> __license__ = '<STR_LIT>' <EOL> __copyright__ = '<STR_LIT>' <EOL> def safe_json ( obj ) : <EOL> return mark_safe ( json . dumps ( obj ) ) <EOL> def update ( d , u ) : <EOL> """<STR_LIT>""" <EOL> for k , v in u . iteritems ( ) : <EOL> if isinstance ( v , collections . Mapping ) : <EOL> r = update ( d . get ( k , { } ) , v ) <EOL> d [ k ] = r <EOL> else : <EOL> d [ k ] = u [ k ] <EOL> return d <EOL> class MissingDataException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class DuplicateLabelException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> LINE_TYPES = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class Flot ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _series = [ ] <EOL> self . _options = { } <EOL> bases = list ( inspect . getmro ( self . __class__ ) ) <EOL> bases . reverse ( ) <EOL> for base in bases : <EOL> if hasattr ( base , '<STR_LIT>' ) : <EOL> update ( self . _options , base . options ) <EOL> @ property <EOL> def series_json ( self ) : <EOL> """<STR_LIT>""" <EOL> return safe_json ( [ self . prepare_series ( s ) for s in self . _series ] ) <EOL> @ property <EOL> def options_json ( self ) : <EOL> """<STR_LIT>""" <EOL> return safe_json ( self . _options ) <EOL> def __getattr__ ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value . startswith ( '<STR_LIT>' ) : <EOL> if value . split ( '<STR_LIT:_>' ) [ <NUM_LIT:1> ] in LINE_TYPES : <EOL> return partial ( self . add_series_type , value [ <NUM_LIT:4> : ] ) <EOL> raise AttributeError <EOL> def add_series_type ( self , line_type , series , label = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> method = getattr ( self , '<STR_LIT>' ) <EOL> return method ( series , label , ** { line_type : True } ) <EOL> def add_series ( self , series , label = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not series : <EOL> raise MissingDataException <EOL> testatom = series [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> if isinstance ( testatom , date ) : <EOL> series = [ ( int ( time . mktime ( ts . timetuple ( ) ) * <NUM_LIT:1000> ) , val ) for ts , val in series ] <EOL> self . _options [ '<STR_LIT>' ] = { '<STR_LIT>' : '<STR_LIT:time>' } <EOL> new_series = { '<STR_LIT:data>' : series } <EOL> if label and label in [ x . get ( '<STR_LIT:label>' , None ) for x in self . _series ] : <EOL> raise DuplicateLabelException <EOL> elif label : <EOL> new_series . update ( label = label ) <EOL> for line_type in LINE_TYPES : <EOL> if line_type in kwargs : <EOL> if isinstance ( kwargs [ line_type ] , collections . Mapping ) : <EOL> new_series . update ( { line_type : kwargs [ line_type ] } ) <EOL> else : <EOL> new_series . update ( { line_type : { '<STR_LIT>' : True } } ) <EOL> self . _series . append ( new_series ) <EOL> def calculate_bar_width ( self ) : <EOL> """<STR_LIT>""" <EOL> slices = max ( [ len ( s [ '<STR_LIT:data>' ] ) for s in self . _series ] ) <EOL> xs = [ pair [ <NUM_LIT:0> ] for pair in chain ( * [ s [ '<STR_LIT:data>' ] for s in self . _series ] ) ] <EOL> xmin , xmax = ( min ( xs ) , max ( xs ) ) <EOL> w = xmax - xmin <EOL> return float ( w ) / slices <EOL> def prepare_series ( self , series ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in series : <EOL> w = self . calculate_bar_width ( ) <EOL> if w : <EOL> series [ '<STR_LIT>' ] [ '<STR_LIT>' ] = w <EOL> return series </s>
