コード例 #1
0
def main():
    # parameters from which the user can choose
    context = {
        "districts": ["1", "2", "3"],
        "initialization_options": ["greedy", "random", "cluster"],
        "optimization_options": ["none", "hillclimber", "simulated annealing"],
        "bools": ["yes", "no"]
    }

    # welcome user
    print("\nWelcome to SmartGrid\n")

    # let user choose which parameters to use
    initialization, district, share_grid, optimization, optimization_type, advanced = get_user_input(
        context)

    # initial configuration
    config1 = Configuration(initialization, district, share_grid, advanced)

    # plot the configuration grid when user wants no optimization
    if optimization == "none":
        config1.make_plot(results_directory, optimization)

    elif optimization == "hillclimber":
        # create HC object with current parameters
        HC = HillClimber(config1.batteries, optimization_type, share_grid)

        # generate figure with the change in costs over all iterations
        HC.plot_costs(results_directory, optimization, config1.district_nr)

        # update batteries in config1 and plot results
        config1.batteries = HC.batteries
        config1.make_plot(results_directory, optimization)

    else:  # optimization == simulated annealing
        # create SA object with current parameters
        SA = SimulatedAnnealing(config1.batteries, share_grid)

        # generate figure with the change in costs over all iterations
        SA.plot_costs(results_directory, optimization, config1.district_nr)

        # update batteries in config1 and plot results
        config1.batteries = SA.batteries
        config1.make_plot(results_directory, optimization)
        return 1

    if not share_grid:
        output = get_output(config1.batteries)
    else:
        output = get_output_shared(config1)

    print(json.dumps(output, indent=4))
コード例 #2
0
def get_weather() -> str:
    while True:
        city_id = input('Input city id (q to quit): ')
        if city_id == 'q':
            break
        units = input('Input units (metric or imperial, default Kelvin): ')
        try:
            data = api.get_result(api.build_url(city_id, units))
            if units == 'metric':
                output.get_output(data, 'C')
            elif units == 'imperial':
                output.get_output(data, 'F')
            else:
                output.get_output(data, 'K')
        except:
            print('Error')
コード例 #3
0
def set_output_and_status(check_result):
    """ get output, compute exit_code an return it """
    start_time = time.time()
    # Check if the service is in database
    if check_result.get('db_data') is None:
        # This is a really strange problem
        # You should never see this error
        logger.warning("[SnmpBooster] [code 0501] No data found in cache. "
                       "Impossible to know which host and service is "
                       "impacted")
        output = "No Data found in cache"
        exit_code = 3

    # Check if all oids in the current service have an error
    elif all([
            ds_data.get('error')
            for ds_data in check_result['db_data']['ds'].values()
    ]):
        # Each ds_data.get('error') is a string
        # ds_data.get('error') == None means No error
        # If all oids have an error, We show only the first one
        random_data = [
            ds_data.get('error')
            for ds_data in check_result['db_data']['ds'].values()
            if ds_data.get('error') is not None
        ]
        output = random_data[0]
        exit_code = 3
    else:
        # Check if mapping is done
        if check_result['db_data'].get('instance') is None \
           and check_result['db_data'].get('mapping') is not None:
            # Mapping is not done
            output = ("Mapping of instance '%s' not "
                      "done" % check_result['db_data'].get('instance_name'))
            logger.warning("[SnmpBooster] [code 0502] [%s, %s]: "
                           "%s" % (
                               check_result['db_data'].get('host'),
                               check_result['db_data'].get('service'),
                               output,
                           ))
            exit_code = 3
        else:
            # If the mapping is done
            # Get output
            output = get_output(check_result['db_data'])
            # Handle triggers
            if check_result['db_data'].get('triggers', {}) != {}:
                error_message, exit_code = get_trigger_result(
                    check_result['db_data'])
                # Handle errors
                if error_message is not None:
                    output = "TRIGGER ERROR: '%s' - %s" % (str(error_message),
                                                           output)
            else:
                exit_code = 0

    # Set state
    check_result['state'] = 'done'
    # Set exit code
    check_result['exit_code'] = exit_code
    # Set output
    check_result['output'] = output
    # Set execution time
    check_result['execution_time'] = check_result[
        'execution_time'] + time.time() - start_time
コード例 #4
0
def set_output_and_status(check_result):
    """ get output, compute exit_code an return it """
    start_time = time.time()
    # Check if the service is in database
    if check_result.get('db_data') is None:
        # This is a really strange problem
        # You should never see this error
        logger.warning("[SnmpBooster] [code 0501] No data found in cache. "
                       "Impossible to know which host and service is "
                       "impacted")
        output = "No Data found in cache"
        exit_code = 3

    # Check if all oids in the current service have an error
    elif all([ds_data.get('error')
              for ds_data in check_result['db_data']['ds'].values()]):
        # Each ds_data.get('error') is a string
        # ds_data.get('error') == None means No error
        # If all oids have an error, We show only the first one
        random_data = [ds_data.get('error')
                       for ds_data in check_result['db_data']['ds'].values()
                       if ds_data.get('error') is not None
                       ]
        output = random_data[0]
        exit_code = 3
    else:
        # Check if mapping is done
        if check_result['db_data'].get('instance') is None \
           and check_result['db_data'].get('mapping') is not None:
            # Mapping is not done
            output = ("Mapping of instance '%s' not "
                      "done" % check_result['db_data'].get('instance_name'))
            logger.warning("[SnmpBooster] [code 0502] [%s, %s]: "
                           "%s" % (check_result['db_data'].get('host'),
                                   check_result['db_data'].get('service'),
                                   output,
                                   )
                           )
            exit_code = 3
        else:
            # If the mapping is done
            # Get output
            output = get_output(check_result['db_data'])
            # Handle triggers
            if check_result['db_data'].get('triggers', {}) != {}:
                error_message, exit_code = get_trigger_result(check_result['db_data'])
                # Handle errors
                if error_message is not None:
                    output = "TRIGGER ERROR: '%s' - %s" % (str(error_message),
                                                           output)
            else:
                exit_code = 0

    # Set state
    check_result['state'] = 'done'
    # Set exit code
    check_result['exit_code'] = exit_code
    # Set output
    check_result['output'] = output
    # Set execution time
    check_result['execution_time'] = check_result['execution_time'] + time.time() - start_time