MapRangeValues

This script converts an input value into another value using two lists. The input value or range is searched in the first list (map_from). If it exists, the value at the same index from the second list (map_to) is returned. If there is no match, the original value is returned. This script supports mapping from either ranges of float numbers or text strings. Example 1: map_from = "1,2,3,4" map_to = "4,3,2,1" value = 3 Output is "2" Example 2: map_from = "1-3,4" map_to = "5,1" value = 3 Output is "5".

python · Filters And Transformers

Details

IDMapRangeValues
Languagepython
From Version6.1.0
Docker Imagedemisto/python3:3.12.13.10404775
Tagstransformer

README

This script converts an input value into another value using two lists. The input value or range is searched in the first list (map_from).
If it exists, the value at the same index from the second list (map_to) is returned. If there is no match, the original value is returned.
This script supports mapping from either ranges of float numbers or text strings.

Example 1:

map_from = “1,2,3,4”
map_to = “4,3,2,1”
value = 3

Output is “2”

Example 2:

map_from = “1-3,4”
map_to = “5,1”
value = 3

Output is “5”

map_from = “0,0.5,1,2,3,4”
map_to = “Unknown,Informational,Low,Medium,High,Critical”
value = 3

Output is “High”

map_from = “Unknown,Informational,Low,Medium,High,Critical”
map_to = “0,0.5,1,2,3,4”
value = Informational

Output is “0.5”

Script Data


Name Description
Script Type python3
Tags transformer
Cortex XSOAR Version 6.1.0

Inputs


Argument Name Description
map_from A comma-separated list of values to map from.
map_to A comma-separated list of values to map to.
sep The separator between the start and end of range values.
value the input value to map.

Outputs


There are no outputs for this script.

import pytest
from MapRangeValues import RangeReplace, Replace, get_replace_list, replace_values

data_test_replace_class = [
    (1, 1, True),
    (1, 4, False),
    ("a", "a", True),
    ("a", "c", False),
]


@pytest.mark.parametrize("_from, value, should_replace", data_test_replace_class)
def test_replace_class(_from, value, should_replace):
    replace_obg = Replace(_from, 1)
    assert replace_obg.should_replace(value) is should_replace


data_test_range_replace_class = [
    (1, 4, 1, True),
    (1, 3, 4, False),
    ("a", "b", "a", True),
    ("a", "b", "c", False),
]


@pytest.mark.parametrize("start, end, value, should_replace", data_test_range_replace_class)
def test_rage_replace_class(start, end, value, should_replace):
    replace_obg = RangeReplace(start, end, 1)
    assert replace_obg.should_replace(value) is should_replace


data_test_get_typed_value = [("a", str), ("1", int), (1, int), ("1.5", float), (1.5, float), ("10.0", int)]


@pytest.mark.parametrize("str_value, output_type", data_test_get_typed_value)
def test_get_typed_value(str_value, output_type):
    assert isinstance(Replace.get_typed_value(str_value), output_type)


data_test_get_replace_list = [
    (
        ["1", "2"],
        ["2", "3"],
        "-",
        [
            Replace("1", "2"),
            Replace("2", "3"),
        ],
    ),
    (
        ["1", "2", "3-5"],
        ["2", "3", "4"],
        "-",
        [
            Replace("1", "2"),
            Replace("2", "3"),
            RangeReplace("3", "5", "4"),
        ],
    ),
    (["a", "b-c"], ["b", "d"], "-", [Replace("a", "b"), RangeReplace("b", "c", "d")]),
    (
        ["1", "2", "3-5", "a", "b-c"],
        ["2", "3", "4", "b", "d"],
        "-",
        [Replace("1", "2"), Replace("2", "3"), RangeReplace("3", "5", "4"), Replace("a", "b"), RangeReplace("b", "c", "d")],
    ),
    (
        ["1", "2", "3§5", "a", "b§c"],
        ["2", "3", "4", "b", "d"],
        "§",
        [Replace("1", "2"), Replace("2", "3"), RangeReplace("3", "5", "4"), Replace("a", "b"), RangeReplace("b", "c", "d")],
    ),
]


@pytest.mark.parametrize("map_from, map_to, sep, expected_replace_list", data_test_get_replace_list)
def test_get_replace_list(map_from, map_to, sep, expected_replace_list):
    for expected_replace, replace in zip(expected_replace_list, get_replace_list(map_from, map_to, sep)):
        assert expected_replace.__dict__ == replace.__dict__


data_test_replace_values = [
    (["1"], [Replace("1", "2")], [2]),
    (["3", "4", "5"], [RangeReplace("3", "5", "4")], [4] * 3),
    (["0.5", "2"], [RangeReplace("0.3", "0.6", "test"), Replace("2", "test2")], ["test", "test2"]),
    ([str(i) for i in range(1, 6)], [Replace("1", "2"), RangeReplace("3", "5", "4")], [2, 2, 4, 4, 4]),
]


@pytest.mark.parametrize("values_list, replace_list, expected_values", data_test_replace_values)
def test_replace_values(values_list, replace_list, expected_values):
    assert expected_values == replace_values(values_list, replace_list)