<commit_before>import decimal
import sanity


def build_kal_scan_band_string(kal_bin, band, args):
    option_mapping = {"gain": "-g",
                      "device": "-d",
                      "error": "-e"}
    if not sanity.scan_band_is_valid(band):
        err_txt = "Unsupported band designation: %" % band
        raise ValueError(err_txt)
    base_string = "%s -v -s %s" % (kal_bin, band)
    for option, flag in option_mapping.items():
        if option in args:
            base_string += str(" %s %s" % (flag, str(args[option])))
    return(base_string)


def build_kal_scan_channel_string(kal_bin, channel, args):
    option_mapping = {"gain": "-g",
                      "device": "-d",
                      "error": "-e"}
    base_string = "%s -v -c %s" % (kal_bin, channel)
    for option, flag in option_mapping.items():
        if option in args:
            base_string += str(" %s %s" % (flag, str(args[option])))
    return(base_string)


def herz_me(val):
    result = 0
    if val.endswith("MHz"):
        stripped = val.replace("MHz", "")
        strip_fl = float(stripped)
        result = strip_fl * 1000000
    elif val.endswith("kHz"):
        stripped = val.replace("kHz", "")
        strip_fl = float(stripped)
        result = strip_fl * 1000
    elif val.endswith("Hz"):
        stripped = val.replace("Hz", "")
        result = float(stripped)
    return(result)


def determine_final_freq(base, direction, modifier):
    result = 0
    if direction == "+":
        result = base + modifier
    elif direction == "-":
        result = base - modifier
    return(result)


def to_eng(num_in):
    x = decimal.Decimal(str(num_in))
    eng_not = x.normalize().to_eng_string()
    return(eng_not)


def determine_scan_band(kal_out):
    band = ""
    while band == "":
        for line in kal_out.splitlines():
            if "kal: Scanning for " in line:
                band = line.split()[3]
        if band == "":
            band = None
    return band


def determine_device(kal_out):
    device = ""
    while device == "":
        for line in kal_out.splitlines():
            if "Using device " in line:
                device = str(line.split(' ', 2)[-1])
        if device == "":
            device = None
    return device


def determine_scan_gain(kal_out):
    gain = ""
    while gain == "":
        for line in kal_out.splitlines():
            if "Setting gain: " in line:
                gain = str(line.split()[2])
        if gain == "":
            gain = None
    return gain


def determine_sample_rate(kal_out):
    sample_rate = ""
    while sample_rate == "":
        for line in kal_out.splitlines():
            if "Exact sample rate" in line:
                sample_rate = str(line.split()[-2])
        if sample_rate == "":
            sample_rate = None
    return sample_rate


def determine_chan_detect_threshold(kal_out):
    channel_detect_threshold = ""
    while channel_detect_threshold == "":
        for line in kal_out.splitlines():
            if "channel detect threshold: " in line:
                channel_detect_threshold = str(line.split()[-1])
        if channel_detect_threshold == "":
            print "Unable to parse sample rate"
            channel_detect_threshold = None
    return channel_detect_threshold


def determine_band_channel(kal_out):
    band = ""
    channel = ""
    tgt_freq = ""
    while band == "":
        for line in kal_out.splitlines():
            if "Using " in line and " channel " in line:
                band = str(line.split()[1])
                channel = str(line.split()[3])
                tgt_freq = str(line.split()[4]).replace(
                    "(", "").replace(")", "")
        if band == "":
            band = None
    return(band, channel, tgt_freq)


def parse_kal_scan(kal_out):
    kal_data = []
    scan_band = determine_scan_band(kal_out)
    scan_gain = determine_scan_gain(kal_out)<commit_msg>chg: dev: Refactor code in fn.py<commit_after>import decimal
import sanity


def options_string_builder(option_mapping, args):
    options_string = ""
    for option, flag in option_mapping.items():
        if option in args:
            options_string += str(" %s %s" % (flag, str(args[option])))
    return options_string


def build_kal_scan_band_string(kal_bin, band, args):
    option_mapping = {"gain": "-g",
                      "device": "-d",
                      "error": "-e"}
    if not sanity.scan_band_is_valid(band):
        err_txt = "Unsupported band designation: %" % band
        raise ValueError(err_txt)
    base_string = "%s -v -s %s" % (kal_bin, band)
    base_string += options_string_builder(option_mapping, args)
    return(base_string)


def build_kal_scan_channel_string(kal_bin, channel, args):
    option_mapping = {"gain": "-g",
                      "device": "-d",
                      "error": "-e"}
    base_string = "%s -v -c %s" % (kal_bin, channel)
    base_string += options_string_builder(option_mapping, args)
    return(base_string)


def herz_me(val):
    result = 0
    if val.endswith("MHz"):
        stripped = val.replace("MHz", "")
        strip_fl = float(stripped)
        result = strip_fl * 1000000
    elif val.endswith("kHz"):
        stripped = val.replace("kHz", "")
        strip_fl = float(stripped)
        result = strip_fl * 1000
    elif val.endswith("Hz"):
        stripped = val.replace("Hz", "")
        result = float(stripped)
    return(result)


def determine_final_freq(base, direction, modifier):
    result = 0
    if direction == "+":
        result = base + modifier
    elif direction == "-":
        result = base - modifier
    return(result)


def to_eng(num_in):
    x = decimal.Decimal(str(num_in))
    eng_not = x.normalize().to_eng_string()
    return(eng_not)


def determine_scan_band(kal_out):
    band = ""
    while band == "":
        for line in kal_out.splitlines():
            if "kal: Scanning for " in line:
                band = line.split()[3]
        if band == "":
            band = None
    return band


def determine_device(kal_out):
    device = ""
    while device == "":
        for line in kal_out.splitlines():
            if "Using device " in line:
                device = str(line.split(' ', 2)[-1])
        if device == "":
            device = None
    return device


def determine_scan_gain(kal_out):
    return(extract_value_from_output("Setting gain: ", 2, kal_out))


def determine_sample_rate(kal_out):
    return(extract_value_from_output("Exact sample rate", -2, kal_out))


def extract_value_from_output(canary, split_offset, kal_out):
    retval = ""
    while retval == "":
        for line in kal_out.splitlines():
            if canary in line:
                retval = str(line.split()[split_offset])
        if retval == "":
            retval = None
    return retval


def determine_chan_detect_threshold(kal_out):
    channel_detect_threshold = ""
    while channel_detect_threshold == "":
        for line in kal_out.splitlines():
            if "channel detect threshold: " in line:
                channel_detect_threshold = str(line.split()[-1])
        if channel_detect_threshold == "":
            print("Unable to parse sample rate")
            channel_detect_threshold = None
    return channel_detect_threshold


def determine_band_channel(kal_out):
    band = ""
    channel = ""
    tgt_freq = ""
    while band == "":
        for line in kal_out.splitlines():
            if "Using " in line and " channel " in line:
                band = str(line.split()[1])
                channel = str(line.split()[3])
                tgt_freq = str(line.split()[4]).replace(
                    "(", "").replace(")", "")
        if band == "":
            band = None
    return(band, channel, tgt_freq)


def parse_kal_scan(kal_out):
    kal_data = []
    scan_band = determine_scan_band(kal_out)
    scan_gain = determine_scan_gain(kal_out)
{'fraction_non_alphanumeric': 0.1267071320182094, 'fraction_numerical': 0.00556398583712696, 'mean_word_length': 2.2215885947046843, 'pattern_counts': {'":': 38, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3776135', 'n_tokens_mistral': 2473, 'n_tokens_neox': 2387, 'n_words': 581}