Re: [Fuego] [PATCH 8/8] python2to3: use list() for dict.keys(), dict.values() and dict.items()

"Bird, Tim" <[email protected]> Fri, 13 May 2022 22:55:21 +0000
Newsgroups dev.linux.lists.fuego
Message-ID <BYAPR13MB2503305C77BCEBC08623655EFDCA9@BYAPR13MB2503.namprd13.prod.outlook.com>
> -----Original Message-----
> From: [email protected] <[email protected]=
m>
> Sent: Wednesday, April 20, 2022 4:12 AM
> To: Bird, Tim <[email protected]>
> Cc: sireesha <[email protected]>; [email protected]=
tion.org; [email protected];
> [email protected]; [email protected]; Shivanand K=
unijadar <[email protected]>
> Subject: [PATCH 8/8] python2to3: use list() for dict.keys(), dict.values(=
) and dict.items()
>=20
> From: sireesha <[email protected]>
>=20
> python3 returns dict_keys type for dict.keys() which doesn't support
> indexing where as python2 returns a list type which does.
> Use list() on the output of dict.keys() to make the code work with
> python3.

Well, strictly speaking some of the places you changed with this patch
don't really need to be changed.  The Python3 dict.keys() function now
returns an iterator instead of a list (as it did in Python2).  In places wh=
ere
the only usage of the dict.keys() value was in a for loop, the change to
use list() is not needed, and in fact might hurt performance.  However,
some of these are real compatibility issues for using the code with Python3=
.

Overall, this seems like an easy way to shut up linter software (like 2to3)
to ignore any messages about these code sequences in the future.  So even
if they are not needed for code compatibility, these changes do make the
code easier to verify with the linter.

=20
> Signed-off-by: sireesha <[email protected]>
> Signed-off-by: Shivanand Kunijadar <[email protected]>
> ---
>  scripts/check-dependencies           |  2 +-
>  scripts/ftc                          | 10 +++++-----
>  scripts/jdiff                        |  4 ++--
>  scripts/ovgen.py                     |  2 +-
>  scripts/parser/common.py             | 10 +++++-----
>  scripts/parser/prepare_chart_data.py | 16 ++++++++--------
>  tests/Functional.LTP/ltp_process.py  |  2 +-
>  7 files changed, 23 insertions(+), 23 deletions(-)
>=20
> diff --git a/scripts/check-dependencies b/scripts/check-dependencies
> index 72a5f31..583012e 100755
> --- a/scripts/check-dependencies
> +++ b/scripts/check-dependencies
> @@ -218,7 +218,7 @@ def main():
>=20
>      # print the testcases skipped
>      # this is the main output from the program
> -    testcases_to_skip =3D reasons.keys()
> +    testcases_to_skip =3D list(reasons.keys())
>      testcases_to_skip.sort()
>      for testcase in testcases_to_skip:
>          print(testcase)
> diff --git a/scripts/ftc b/scripts/ftc
> index 3723e7e..f32d9cc 100755
> --- a/scripts/ftc
> +++ b/scripts/ftc
> @@ -905,7 +905,7 @@ class test_class:
>      }
>      def __init__(self, conf, test_dict, test_flags=3D{}):
>          merged_defaults =3D dict(self.DEFAULT_DEFAULTS)
> -        for key, value in test_flags.items():
> +        for key, value in list(test_flags.items()):

list() is not needed here
>              merged_defaults[key] =3D value
>          self.name =3D str(test_dict["testName"])
>          self.test_type =3D self.name.split(".")[0]
> @@ -1642,7 +1642,7 @@ def parse_testplan(conf, plan, test_dict):
>              test_flags['postcleanup'] =3D plan['default_postcleanup']
>=20
>          # override with testplan per-test flags
> -        for key, value in plan_test_dict.items():
> +        for key, value in list(plan_test_dict.items()):
list() is not needed here

>              if key =3D=3D "testName":
>                  test_dict[key] =3D value
>                  continue
> @@ -2584,7 +2584,7 @@ def parse_where(where_string):
>=20
>  def filter_runs(run_map, where_list):
>      new_run_list =3D []
> -    for run_id, run in run_map.items():
> +    for run_id, run in list(run_map.items()):
list() is not needed here

>          match =3D True
>          for where in where_list:
>              if not where.match(run):
> @@ -2915,7 +2915,7 @@ def pts_set_style(ws):
>          for cell in row:
>              if cell.value:
>                  dims[cell.column] =3D max((dims.get(cell.column, 0), len=
(str(cell.value)) + 2))
> -    for col, value in dims.items():
> +    for col, value in list(dims.items()):
list() is not needed here()

>          ws.column_dimensions[col].width =3D value
>=20
>=20
> @@ -4767,7 +4767,7 @@ def do_run_test(conf, options):
>                  test_spec_data['specs'][test.spec][key] =3D dyn_vars[key=
]
>=20
>          # track what variables where modified
> -        test_spec_data['specs'][test.spec]['dyn_vars'] =3D dyn_vars.keys=
()
> +        test_spec_data['specs'][test.spec]['dyn_vars'] =3D list(dyn_vars=
.keys())
>      dvar("test_spec_data")
>=20
>      if os.path.isdir(build_data.test_logdir):
> diff --git a/scripts/jdiff b/scripts/jdiff
> index b267afd..39ea1db 100755
> --- a/scripts/jdiff
> +++ b/scripts/jdiff
> @@ -94,9 +94,9 @@ def diff_list(prefix, l1, l2):
>  def diff_map(prefix, m1, m2):
>      dprint("in diff_map, m1=3D%s, m2=3D%s" % (m1, m2))
>=20
> -    l1s =3D m1.keys()
> +    l1s =3D list(m1.keys())
>      l1s.sort()
> -    l2s =3D m2.keys()
> +    l2s =3D list(m2.keys())
>      l2s.sort()
>      dprint("in diff_map, l1s=3D%s, l2s=3D%s" % (l1s, l2s))
>=20
> diff --git a/scripts/ovgen.py b/scripts/ovgen.py
> index 1386ff5..f47a384 100755
> --- a/scripts/ovgen.py
> +++ b/scripts/ovgen.py
> @@ -453,7 +453,7 @@ def parseSpec(logdir, testdir, testspec):
>                  test_spec_data =3D json.load(f)
>              except:
>                  error_out("Error parsing spec file %s" % specpath)
> -        for key in test_spec_data['specs'].keys():
> +        for key in list(test_spec_data['specs'].keys()):
list() is not needed here

>              if key !=3D testspec:
>                  del test_spec_data['specs'][key]
>          debug_print("test spec data:" + str(test_spec_data))
> diff --git a/scripts/parser/common.py b/scripts/parser/common.py
> index 8c307f1..a3d71ca 100644
> --- a/scripts/parser/common.py
> +++ b/scripts/parser/common.py
> @@ -178,7 +178,7 @@ def add_results(results, run_data):
>      dprint("in add_results")
>      if not results:
>          return
> -    for test_case_id in results.keys():
> +    for test_case_id in list(results.keys()):
list() is not needed here.

>          test_case =3D get_test_case(test_case_id, run_data)
>          if not test_case:
>              continue
> @@ -516,7 +516,7 @@ def apply_criteria(run_data, criteria_data):
>  def create_default_ref(results):
>      dprint("in create_default_ref")
>      ref =3D {'test_sets': []}
> -    for test_case_id in results.keys():
> +    for test_case_id in list(results.keys()):
>          test_set_name, test_case_name =3D split_test_id(test_case_id)
>          item =3D results[test_case_id]
>          if isinstance(item, list):
> @@ -553,7 +553,7 @@ def name_compare(a, b):
>  def dump_ordered_data(data, indent=3D""):
>      if type(data)=3D=3Dtype({}):
>          print("%s{" % indent)
> -        keylist =3D data.keys()
> +        keylist =3D list(data.keys())
>          keylist.sort()
>          for key in keylist:
>              print('%s "%s":' % (indent+"  ", key),
> @@ -734,7 +734,7 @@ def process_data(ref_section_pat, test_results, plot_=
type, label):
>      test_name =3D TESTDIR.split(".")[1]
>=20
>      # convert old-style cur_dict into measurements structure
> -    for (old_id, value) in test_results.items():
> +    for (old_id, value) in list(test_results.items()):
list() is not needed here.

>          ts_name, tc_name, measure =3D split_old_id(old_id)
>          test_case_id =3D "%s.%s" % (ts_name, tc_name)
>          new_measure =3D {"name":measure, "measure": float(value)}
> @@ -819,7 +819,7 @@ def split_output_per_testcase (regex_string, measurem=
ents, info_follows_regex=3D0)
>      fd.close()
>=20
>      # note that measurements is an OrderedDict, so keys comes out ordere=
d
> -    testcase_names =3D measurements.keys()
> +    testcase_names =3D list(measurements.keys())
>=20
>      if info_follows_regex:
>          # You can have stuff before the first testcase delimiter that
> diff --git a/scripts/parser/prepare_chart_data.py b/scripts/parser/prepar=
e_chart_data.py
> index d85315e..61b55f5 100644
> --- a/scripts/parser/prepare_chart_data.py
> +++ b/scripts/parser/prepare_chart_data.py
> @@ -386,7 +386,7 @@ def make_measure_plots(test_name, chart_config, entri=
es):
>                  point =3D [entry.build_number, value]
>                  series_map[ref_series_key]["data"].append(point)
>=20
> -        flot_data =3D series_map.values()
> +        flot_data =3D list(series_map.values())
>          flot_data.sort(key=3Ditemgetter('label'))
>=20
>          flot_options =3D {
> @@ -425,7 +425,7 @@ def make_measure_tables(test_name, chart_config, entr=
ies):
>      for entry in entries:
>          bsp_key =3D entry.board + "." + entry.spec + "." + entry.kernel
>          bsp_map[bsp_key] =3D ((entry.board, entry.spec, entry.kernel))
> -    bsp_list =3D bsp_map.values()
> +    bsp_list =3D list(bsp_map.values())
>=20
>      # now make a chart for each one:
>      for board, spec, kver in bsp_list:
> @@ -473,7 +473,7 @@ def make_measure_tables(test_name, chart_config, entr=
ies):
>              else:
>                  build_num_map[entry.build_number][3] +=3D 1
>=20
> -        bn_list =3D build_num_map.keys()
> +        bn_list =3D list(build_num_map.keys())
>          bn_list.sort()
>=20
>          # FIXTHIS - should read col_limit from chart_config
> @@ -506,7 +506,7 @@ def make_measure_tables(test_name, chart_config, entr=
ies):
>          html +=3D row
>=20
>          # one row per test case
> -        tg_list =3D result_map.keys()
> +        tg_list =3D list(result_map.keys())
>          tg_list.sort(key=3Dfunctools.cmp_to_key(cmp_alpha_num))
>=20
>          for tg in tg_list:
> @@ -617,7 +617,7 @@ def make_testcase_table(test_name, chart_config, entr=
ies):
>      for entry in entries:
>          bts_key =3D entry.board + "." + entry.test_set
>          bts_map[bts_key] =3D ((entry.board, entry.test_set))
> -    bts_list =3D bts_map.values()
> +    bts_list =3D list(bts_map.values())
>=20
>      # now make a chart for each one:
>      for board, ts in bts_list:
> @@ -697,7 +697,7 @@ def make_testcase_table(test_name, chart_config, entr=
ies):
>              else:
>                  build_num_map[entry.build_number][3] +=3D 1
>=20
> -        bn_list =3D build_num_map.keys()
> +        bn_list =3D list(build_num_map.keys())
>          bn_list.sort()
>=20
>          # FIXTHIS - should read col_limit from chart_config
> @@ -729,7 +729,7 @@ def make_testcase_table(test_name, chart_config, entr=
ies):
>          html +=3D row
>=20
>          # one row per test case
> -        tc_list =3D result_map.keys()
> +        tc_list =3D list(result_map.keys())
>          tc_list.sort(key=3Dfunctools.cmp_to_key(cmp_alpha_num))
>=20
>          for tc in tc_list:
> @@ -885,7 +885,7 @@ def make_testset_summary_table(test_name, chart_confi=
g, entries):
>          html +=3D row
>=20
>          # one row per spec/build_number/test set
> -        ssb_list =3D ssb_map.keys()
> +        ssb_list =3D list(ssb_map.keys())
>          ssb_list.sort(ssb_cmp)
>=20
>          # calculate rowspan for each spec and spec/build_number combo
> diff --git a/tests/Functional.LTP/ltp_process.py b/tests/Functional.LTP/l=
tp_process.py
> index 8a29830..8e2b637 100644
> --- a/tests/Functional.LTP/ltp_process.py
> +++ b/tests/Functional.LTP/ltp_process.py
> @@ -269,7 +269,7 @@ def pts_set_style(ws):
>          for cell in row:
>              if cell.value:
>                  dims[cell.column] =3D max((dims.get(cell.column, 0), len=
(cell.value) + 2))
> -    for col, value in dims.items():
> +    for col, value in list(dims.items()):
list() is not needed here.

>          ws.column_dimensions[col].width =3D value
>=20
>  if os.path.exists('pts.log'):
> --
> 2.20.1
>=20

OK - overall this looks good.  It's applied.

Sorry it took so long to get to these.  Thanks very much for the effort to =
fix
these incompatibilities with Python3 submit the patches!!

 -- Tim