def test_eval_bad_3(self): try: result = safe_eval_calculator("(5+6) d* 4 / 3") except ValueError: pass else: self.fail('should raise ValueError')
def test_eval_bad_3(self): try: _ = safe_eval_calculator("(5+6) d* 4 / 3") except ValueError: pass else: self.fail('should raise ValueError')
def test_eval_bad_5(self): try: result = safe_eval_calculator("5***4") except SyntaxError: pass else: self.fail('should raise SyntaxError')
def test_eval_bad_4(self): try: _ = safe_eval_calculator("_2+2") except ValueError: pass else: self.fail('should raise ValueError')
def test_eval_bad_5(self): try: _ = safe_eval_calculator("5***4") except SyntaxError: pass else: self.fail('should raise SyntaxError')
def test_eval_bad_2(self): try: _ = safe_eval_calculator("a5+6 * 4 / 3") except ValueError: pass else: self.fail('should raise ValueError')
def test_eval_good_3(self): self.assertEqual(safe_eval_calculator("5+6 * 4 / 3"), 13, "Wrong safe eval result: %s" % safe_eval_calculator("5+6 * 4 / 3"))
def test_eval_good_2(self): self.assertEqual(safe_eval_calculator("2 * 10"), 20, "Wrong safe eval result: %s" % safe_eval_calculator("2*10"))
def check_arguments(self, args): """Parse all command lines options. could also exit from program because of wrong arguments """ parsed_config_files = DipplannerConfigFiles(args.config_files) dives = parsed_config_files.dives if dives is None: dives = OrderedDict() if args.gflow: try: settings.GF_LOW = float( safe_eval_calculator(args.gflow.strip('%'))) / 100 except ValueError: self.parser.error("Error while parsing option gflow : %s" % args.gflow) if args.gfhigh: try: settings.GF_HIGH = float( safe_eval_calculator(args.gfhigh.strip('%'))) / 100 except ValueError: self.parser.error("Error while parsing option gfhigh: %s" % args.gfhigh) if args.water: if args.water.lower() == "sea": settings.WATER_DENSITY = settings.SEA_WATER_DENSITY if args.water.lower() == "fresh": settings.WATER_DENSITY = settings.FRESH_WATER_DENSITY if args.altitude: settings.AMBIANT_PRESSURE_SURFACE = altitude_to_pressure( args.altitude) if args.diveconsrate: try: settings.DIVE_CONSUMPTION_RATE = float( safe_eval_calculator(args.diveconsrate)) / 60 except ValueError: self.parser.error("Error while parsing option " "diveconsrate : %s" % args.diveconsrate) if args.decoconsrate: try: settings.DECO_CONSUMPTION_RATE = float( safe_eval_calculator(args.decoconsrate)) / 60 except ValueError: self.parser.error("Error while parsing option " "decoconsrate : %s" % args.decoconsrate) if args.descentrate: try: settings.DESCENT_RATE = float( safe_eval_calculator(args.descentrate)) / 60 except ValueError: self.parser.error("Error while parsing option " "descentrate : %s" % args.descentrate) if args.ascentrate: try: settings.ASCENT_RATE = float( safe_eval_calculator(args.ascentrate)) / 60 except ValueError: self.parser.error("Error while parsing option " "ascentrate : %s" % args.ascentrate) if args.deco_ascentrate: try: settings.DECO_ASCENT_RATE = float( safe_eval_calculator(args.deco_ascentrate)) / 60 except ValueError: self.parser.error( "Error while parsing option " "deco_ascentrate : %s" % args.deco_ascentrate) if args.model: settings.DECO_MODEL = args.model if args.maxppo2: settings.DEFAULT_MAX_PPO2 = args.maxppo2 if args.minppo2: settings.DEFAULT_MIN_PPO2 = args.minppo2 if args.maxend: settings.DEFAULT_MAX_END = args.maxend if args.samegasfordeco: settings.USE_OC_DECO = False if args.forcesegmenttime: settings.RUN_TIME = False if args.depthcalcmethod in ('simple', 'complex'): settings.METHOD_FOR_DEPTH_CALCULATION = args.depthcalcmethod if args.travelswitch in ('late', 'early'): settings.TRAVEL_SWITCH = args.travelswitch if args.surfacetemp is not None: settings.SURFACE_TEMP = args.surfacetemp if args.ambiantpressureatsea: settings.AMBIANT_PRESSURE_SEA_LEVEL = args.ambiantpressureatsea if args.multilevel: settings.MULTILEVEL_MODE = True if args.automatictankrefill: settings.AUTOMATIC_TANK_REFILL = True if args.notankrefill: settings.AUTOMATIC_TANK_REFILL = False # try to find tank(s) and segment(s). # if found, add this dive to the (eventually) other dives defined # in config files. # this will be the last dive tanks = {} if args.tanks: for tank in args.tanks: (name, f_o2, f_he, volume, pressure, rule) = tank.split(";") tanks[name] = Tank( float(f_o2), float(f_he), max_ppo2=settings.DEFAULT_MAX_PPO2, tank_vol=float(safe_eval_calculator(volume)), tank_pressure=float(safe_eval_calculator(pressure)), tank_rule=rule) if tanks == {}: # no tank provided, try to get the previous tanks try: tanks = dives[list(dives.items())[-1][0]]['tanks'] except (KeyError, IndexError): self.parser.error("Error : no tank provided for this dive !") segments = OrderedDict() if args.segments: num_seg = 1 for seg in args.segments: (depth, time, tankname, setpoint) = seg.split(";") # looks for tank in tanks try: # seg_name = 'segment%s' % num_seg # print(seg_name) segments['segment%s' % num_seg] = SegmentDive( float(safe_eval_calculator(depth)), float(safe_eval_calculator(time)), tanks[tankname], float(setpoint)) except KeyError: self.parser.error( "Error : tank name (%s) in not found in tank list !" % tankname) num_seg += 1 segments = OrderedDict(sorted(segments.items(), key=lambda t: t[0])) if args.surfaceinterval: dives['diveCLI'] = {'tanks': tanks, 'segments': segments, 'surface_interval': safe_eval_calculator(args.surfaceinterval)} else: dives['diveCLI'] = {'tanks': tanks, 'segments': segments, 'surface_interval': 0} if args.template: settings.TEMPLATE = args.template # returns self.args = args self.dives = dives
def check_configs_general_section(self): """Check configs and change default settings values.""" config = self.config if config.has_section('general'): section = 'general' if config.has_option(section, 'deco_model'): model = ''.join(config.get(section, 'deco_model')).strip() if model in ("ZHL16a", "ZHL16b", "ZHL16c"): settings.DECO_MODEL = model if config.has_option(section, 'gf_low'): settings.GF_LOW = float( safe_eval_calculator(''.join(config.get( section, 'gf_low')).strip('%'))) / 100 if config.has_option(section, 'gf_high'): settings.GF_HIGH = float( safe_eval_calculator(''.join(config.get( section, 'gf_high')).strip('%'))) / 100 if config.has_option(section, 'water'): if config.get(section, 'water') == 'sea': settings.WATER_DENSITY = settings.SEA_WATER_DENSITY elif config.get(section, 'water') == 'fresh': settings.WATER_DENSITY = settings.FRESH_WATER_DENSITY if config.has_option(section, 'altitude'): settings.AMBIANT_PRESSURE_SURFACE = altitude_to_pressure( float(config.get(section, 'altitude'))) if config.has_option(section, 'dive_consumption_rate'): settings.DIVE_CONSUMPTION_RATE = float( config.get(section, 'dive_consumption_rate')) / 60 if config.has_option(section, 'deco_consumption_rate'): settings.DECO_CONSUMPTION_RATE = float( config.get(section, 'deco_consumption_rate')) / 60 if config.has_option(section, 'descent_rate'): settings.DESCENT_RATE = float( config.get(section, 'descent_rate')) / 60 if config.has_option(section, 'ascent_rate'): settings.ASCENT_RATE = float( config.get(section, 'ascent_rate')) / 60 if config.has_option(section, 'deco_ascent_rate'): settings.DECO_ASCENT_RATE = float( config.get(section, 'deco_ascent_rate')) / 60 if config.has_option(section, 'max_ppo2'): settings.DEFAULT_MAX_PPO2 = float(config.get(section, 'max_ppo2')) if config.has_option(section, 'min_ppo2'): settings.DEFAULT_MIN_PPO2 = float(config.get(section, 'min_ppo2')) if config.has_option(section, 'max_end'): settings.DEFAULT_MAX_END = float(config.get(section, 'max_end')) if config.has_option(section, 'run_time'): settings.RUN_TIME = config.getboolean(section, 'run_time') if config.has_option(section, 'use_oc_deco'): settings.USE_OC_DECO = config.getboolean(section, 'use_oc_deco') if config.has_option(section, 'multilevel_mode'): settings.MULTILEVEL_MODE = config.getboolean(section, 'multilevel_mode') if config.has_option(section, 'automatic_tank_refill'): settings.AUTOMATIC_TANK_REFILL = config.getboolean( section, 'automatic_tank_refill')
def check_configs_dives_section(self): """Check configs and change default settings values.""" config = self.config # dives = { 'dive1': { 'tanks': {}, # 'segments': {}, # 'surface_interval':0} } dives = {} dive_number = 1 # initialization while config.has_section('dive%s' % dive_number): section = 'dive%s' % dive_number dives[section] = {'tanks': {}, 'segments': {}, 'surface_interval': 0} for parameter_name, parameter_value in config.items(section): if parameter_name == 'surface_interval': dives[section]['surface_interval'] = safe_eval_calculator( parameter_value) elif parameter_name[0:4] == 'tank': # number = parameter_name[4:] (name, f_o2, f_he, volume, pressure, rule) = parameter_value.split(";") dives[section]['tanks'][name] = Tank( float(f_o2), float(f_he), max_ppo2=settings.DEFAULT_MAX_PPO2, tank_vol=float(safe_eval_calculator(volume)), tank_pressure=float(safe_eval_calculator(pressure)), tank_rule=rule) if dives[section]['tanks'] == {}: # no tank provided, try to get the previous tanks try: dives[section]['tanks'] = dives[ 'dive%s' % (dive_number - 1)]['tanks'] except KeyError: print("Error : no tank provided for this dive !") sys.exit(0) for param_name, param_value in config.items(section): if param_name[0:7] == 'segment': # number = parameter_name[4:] (depth, time, tankname, setpoint) = param_value.split(";") try: dives[section]['segments'][param_name] = SegmentDive( float(safe_eval_calculator(depth)), float(safe_eval_calculator(time)), dives[section]['tanks'][tankname], float(setpoint)) except KeyError: print("Error : tank name (%s) in not found" " in tank list !" % tankname) sys.exit(0) except: raise dives[section]['segments'] = OrderedDict( sorted(dives[section]['segments'].items(), key=lambda t: t[0])) dive_number += 1 self.dives = OrderedDict(sorted(dives.items(), key=lambda t: t[0]))
def check_configs_general_section(self): """Check configs and change default settings values.""" config = self.config if config.has_section('general'): section = 'general' if config.has_option(section, 'deco_model'): model = ''.join(config.get(section, 'deco_model')).strip() if model in ("ZHL16a", "ZHL16b", "ZHL16c"): settings.DECO_MODEL = model if config.has_option(section, 'gf_low'): settings.GF_LOW = float( safe_eval_calculator(''.join(config.get( section, 'gf_low')).strip('%'))) / 100 if config.has_option(section, 'gf_high'): settings.GF_HIGH = float( safe_eval_calculator(''.join(config.get( section, 'gf_high')).strip('%'))) / 100 if config.has_option(section, 'water'): if config.get(section, 'water') == 'sea': settings.WATER_DENSITY = settings.SEA_WATER_DENSITY elif config.get(section, 'water') == 'fresh': settings.WATER_DENSITY = settings.FRESH_WATER_DENSITY if config.has_option(section, 'altitude'): settings.AMBIANT_PRESSURE_SURFACE = altitude_to_pressure( float(config.get(section, 'altitude'))) if config.has_option(section, 'dive_consumption_rate'): settings.DIVE_CONSUMPTION_RATE = float( config.get(section, 'dive_consumption_rate')) / 60 if config.has_option(section, 'deco_consumption_rate'): settings.DECO_CONSUMPTION_RATE = float( config.get(section, 'deco_consumption_rate')) / 60 if config.has_option(section, 'descent_rate'): settings.DESCENT_RATE = float( config.get(section, 'descent_rate')) / 60 if config.has_option(section, 'ascent_rate'): settings.ASCENT_RATE = float(config.get( section, 'ascent_rate')) / 60 if config.has_option(section, 'deco_ascent_rate'): settings.DECO_ASCENT_RATE = float( config.get(section, 'deco_ascent_rate')) / 60 if config.has_option(section, 'max_ppo2'): settings.DEFAULT_MAX_PPO2 = float( config.get(section, 'max_ppo2')) if config.has_option(section, 'min_ppo2'): settings.DEFAULT_MIN_PPO2 = float( config.get(section, 'min_ppo2')) if config.has_option(section, 'max_end'): settings.DEFAULT_MAX_END = float(config.get( section, 'max_end')) if config.has_option(section, 'run_time'): settings.RUN_TIME = config.getboolean(section, 'run_time') if config.has_option(section, 'use_oc_deco'): settings.USE_OC_DECO = config.getboolean( section, 'use_oc_deco') if config.has_option(section, 'multilevel_mode'): settings.MULTILEVEL_MODE = config.getboolean( section, 'multilevel_mode') if config.has_option(section, 'automatic_tank_refill'): settings.AUTOMATIC_TANK_REFILL = config.getboolean( section, 'automatic_tank_refill')
def check_configs_dives_section(self): """Check configs and change default settings values.""" config = self.config # dives = { 'dive1': { 'tanks': {}, # 'segments': {}, # 'surface_interval':0} } dives = {} dive_number = 1 # initialization while config.has_section('dive%s' % dive_number): section = 'dive%s' % dive_number dives[section] = { 'tanks': {}, 'segments': {}, 'surface_interval': 0 } for parameter_name, parameter_value in config.items(section): if parameter_name == 'surface_interval': dives[section]['surface_interval'] = safe_eval_calculator( parameter_value) elif parameter_name[0:4] == 'tank': # number = parameter_name[4:] (name, f_o2, f_he, volume, pressure, rule) = parameter_value.split(";") dives[section]['tanks'][name] = Tank( float(f_o2), float(f_he), max_ppo2=settings.DEFAULT_MAX_PPO2, tank_vol=float(safe_eval_calculator(volume)), tank_pressure=float(safe_eval_calculator(pressure)), tank_rule=rule) if dives[section]['tanks'] == {}: # no tank provided, try to get the previous tanks try: dives[section]['tanks'] = dives['dive%s' % (dive_number - 1)]['tanks'] except KeyError: print("Error : no tank provided for this dive !") sys.exit(0) for param_name, param_value in config.items(section): if param_name[0:7] == 'segment': # number = parameter_name[4:] (depth, time, tankname, setpoint) = param_value.split(";") try: dives[section]['segments'][param_name] = SegmentDive( float(safe_eval_calculator(depth)), float(safe_eval_calculator(time)), dives[section]['tanks'][tankname], float(setpoint)) except KeyError: print("Error : tank name (%s) in not found" " in tank list !" % tankname) sys.exit(0) except: raise dives[section]['segments'] = OrderedDict( sorted(dives[section]['segments'].items(), key=lambda t: t[0])) dive_number += 1 self.dives = OrderedDict(sorted(dives.items(), key=lambda t: t[0]))
def test_eval_good_3(self): self.assertEqual( safe_eval_calculator("5+6 * 4 / 3"), 13, "Wrong safe eval result: %s" % safe_eval_calculator("5+6 * 4 / 3"))
def test_eval_good_2(self): self.assertEqual( safe_eval_calculator("2 * 10"), 20, "Wrong safe eval result: %s" % safe_eval_calculator("2*10"))
def check_arguments(self, args): """Parse all command lines options. could also exit from program because of wrong arguments """ parsed_config_files = DipplannerConfigFiles(args.config_files) dives = parsed_config_files.dives if dives is None: dives = OrderedDict() if args.gflow: try: settings.GF_LOW = float( safe_eval_calculator(args.gflow.strip('%'))) / 100 except ValueError: self.parser.error("Error while parsing option gflow : %s" % args.gflow) if args.gfhigh: try: settings.GF_HIGH = float( safe_eval_calculator(args.gfhigh.strip('%'))) / 100 except ValueError: self.parser.error("Error while parsing option gfhigh: %s" % args.gfhigh) if args.water: if args.water.lower() == "sea": settings.WATER_DENSITY = settings.SEA_WATER_DENSITY if args.water.lower() == "fresh": settings.WATER_DENSITY = settings.FRESH_WATER_DENSITY if args.altitude: settings.AMBIANT_PRESSURE_SURFACE = altitude_to_pressure( args.altitude) if args.diveconsrate: try: settings.DIVE_CONSUMPTION_RATE = float( safe_eval_calculator(args.diveconsrate)) / 60 except ValueError: self.parser.error("Error while parsing option " "diveconsrate : %s" % args.diveconsrate) if args.decoconsrate: try: settings.DECO_CONSUMPTION_RATE = float( safe_eval_calculator(args.decoconsrate)) / 60 except ValueError: self.parser.error("Error while parsing option " "decoconsrate : %s" % args.decoconsrate) if args.descentrate: try: settings.DESCENT_RATE = float( safe_eval_calculator(args.descentrate)) / 60 except ValueError: self.parser.error("Error while parsing option " "descentrate : %s" % args.descentrate) if args.ascentrate: try: settings.ASCENT_RATE = float( safe_eval_calculator(args.ascentrate)) / 60 except ValueError: self.parser.error("Error while parsing option " "ascentrate : %s" % args.ascentrate) if args.deco_ascentrate: try: settings.DECO_ASCENT_RATE = float( safe_eval_calculator(args.deco_ascentrate)) / 60 except ValueError: self.parser.error("Error while parsing option " "deco_ascentrate : %s" % args.deco_ascentrate) if args.model: settings.DECO_MODEL = args.model if args.maxppo2: settings.DEFAULT_MAX_PPO2 = args.maxppo2 if args.minppo2: settings.DEFAULT_MIN_PPO2 = args.minppo2 if args.maxend: settings.DEFAULT_MAX_END = args.maxend if args.samegasfordeco: settings.USE_OC_DECO = False if args.forcesegmenttime: settings.RUN_TIME = False if args.depthcalcmethod in ('simple', 'complex'): settings.METHOD_FOR_DEPTH_CALCULATION = args.depthcalcmethod if args.travelswitch in ('late', 'early'): settings.TRAVEL_SWITCH = args.travelswitch if args.surfacetemp is not None: settings.SURFACE_TEMP = args.surfacetemp if args.ambiantpressureatsea: settings.AMBIANT_PRESSURE_SEA_LEVEL = args.ambiantpressureatsea if args.multilevel: settings.MULTILEVEL_MODE = True if args.automatictankrefill: settings.AUTOMATIC_TANK_REFILL = True if args.notankrefill: settings.AUTOMATIC_TANK_REFILL = False # try to find tank(s) and segment(s). # if found, add this dive to the (eventually) other dives defined # in config files. # this will be the last dive tanks = {} if args.tanks: for tank in args.tanks: (name, f_o2, f_he, volume, pressure, rule) = tank.split(";") tanks[name] = Tank( float(f_o2), float(f_he), max_ppo2=settings.DEFAULT_MAX_PPO2, tank_vol=float(safe_eval_calculator(volume)), tank_pressure=float(safe_eval_calculator(pressure)), tank_rule=rule) if tanks == {}: # no tank provided, try to get the previous tanks try: tanks = dives[list(dives.items())[-1][0]]['tanks'] except (KeyError, IndexError): self.parser.error("Error : no tank provided for this dive !") segments = OrderedDict() if args.segments: num_seg = 1 for seg in args.segments: (depth, time, tankname, setpoint) = seg.split(";") # looks for tank in tanks try: # seg_name = 'segment%s' % num_seg # print(seg_name) segments['segment%s' % num_seg] = SegmentDive( float(safe_eval_calculator(depth)), float(safe_eval_calculator(time)), tanks[tankname], float(setpoint)) except KeyError: self.parser.error( "Error : tank name (%s) in not found in tank list !" % tankname) num_seg += 1 segments = OrderedDict(sorted(segments.items(), key=lambda t: t[0])) if args.surfaceinterval: dives['diveCLI'] = { 'tanks': tanks, 'segments': segments, 'surface_interval': safe_eval_calculator(args.surfaceinterval) } else: dives['diveCLI'] = { 'tanks': tanks, 'segments': segments, 'surface_interval': 0 } if args.template: settings.TEMPLATE = args.template # returns self.args = args self.dives = dives