Exemple #1
0
def _strs_to_x_1(ss, x_format, none_values, none_result, raise_value_error,
                 return_extra_info, min_success_rate, str_to_x_1):
    if checks.is_string(x_format): x_format = [x_format]
    success_count = 0
    none_count = 0
    total_count = 0
    results = []
    x_format_idxs = []
    for s in ss:
        s = s.strip()
        if s in none_values:
            result = None
            x_format_idx = None
            none_count += 1
        else:
            for x_format_idx, x_frmt in enumerate(x_format):
                try:
                    result = str_to_x_1(s,
                                        x_frmt,
                                        none_values=[],
                                        none_result=none_result,
                                        raise_value_error=True,
                                        return_extra_info=False)
                    success_count += 1
                    break
                except:
                    result, _, x_format_idx = none_result, None, None
        total_count += 1
        results.append(result)
        x_format_idxs.append(x_format_idx)
    if success_count > 0 and float(success_count) / float(
            total_count - none_count) >= min_success_rate:
        most_common_x_format_idx = utils.most_common(
            [i for i in x_format_idxs if i is not None])
        most_common_x_format = x_format[most_common_x_format_idx]
    else:
        if raise_value_error: raise ValueError('Unable to parse strings')
        results, most_common_x_format, most_common_x_format_idx = None, None, None
    if return_extra_info:
        return (results, success_count, none_count, most_common_x_format,
                most_common_x_format_idx)
    else:
        return results
Exemple #2
0
 def test_most_common(self):
     self.assertEqual(
         utils.most_common(['foo', 'bar', 'bar', 'foo', 'bar']), 'bar')
     self.assertEqual(utils.most_common(['foo', 'bar', 'bar', 'foo']),
                      'foo')