Skip to content

Commit 16f8a41

Browse files
authored
Remove code needed for Python <3.6 (#844)
1 parent 02b8f66 commit 16f8a41

File tree

3 files changed

+27
-84
lines changed

3 files changed

+27
-84
lines changed

esmvalcore/_recipe.py

+4-46
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import logging
44
import os
55
import re
6-
from collections import OrderedDict
76
from copy import deepcopy
87
from pprint import pformat
98

@@ -35,60 +34,18 @@
3534
TASKSEP = os.sep
3635

3736

38-
def ordered_safe_load(stream):
39-
"""Load a YAML file using OrderedDict instead of dict."""
40-
class OrderedSafeLoader(yaml.SafeLoader):
41-
"""Loader class that uses OrderedDict to load a map."""
42-
def construct_mapping(loader, node):
43-
"""Load a map as an OrderedDict."""
44-
loader.flatten_mapping(node)
45-
return OrderedDict(loader.construct_pairs(node))
46-
47-
OrderedSafeLoader.add_constructor(
48-
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, construct_mapping)
49-
50-
return yaml.load(stream, OrderedSafeLoader)
51-
52-
53-
def load_raw_recipe(filename):
54-
"""Check a recipe file and return it in raw form."""
55-
# Note that many checks can only be performed after the automatically
56-
# computed entries have been filled in by creating a Recipe object.
37+
def read_recipe_file(filename, config_user, initialize_tasks=True):
38+
"""Read a recipe from file."""
5739
check.recipe_with_schema(filename)
5840
with open(filename, 'r') as file:
59-
contents = file.read()
60-
raw_recipe = yaml.safe_load(contents)
61-
raw_recipe['preprocessors'] = ordered_safe_load(contents).get(
62-
'preprocessors', {})
63-
64-
check.diagnostics(raw_recipe['diagnostics'])
65-
return raw_recipe
66-
41+
raw_recipe = yaml.safe_load(file)
6742

68-
def read_recipe_file(filename, config_user, initialize_tasks=True):
69-
"""Read a recipe from file."""
70-
raw_recipe = load_raw_recipe(filename)
7143
return Recipe(raw_recipe,
7244
config_user,
7345
initialize_tasks,
7446
recipe_file=filename)
7547

7648

77-
def _get_value(key, datasets):
78-
"""Get a value for key by looking at the other datasets."""
79-
values = {dataset[key] for dataset in datasets if key in dataset}
80-
81-
if len(values) > 1:
82-
raise RecipeError("Ambiguous values {} for property {}".format(
83-
values, key))
84-
85-
value = None
86-
if len(values) == 1:
87-
value = values.pop()
88-
89-
return value
90-
91-
9249
def _add_cmor_info(variable, override=False):
9350
"""Add information from CMOR tables to variable."""
9451
logger.debug("If not present: adding keys from CMOR table to %s", variable)
@@ -976,6 +933,7 @@ def _initialize_provenance(self, raw_documentation):
976933
def _initialize_diagnostics(self, raw_diagnostics, raw_datasets):
977934
"""Define diagnostics in recipe."""
978935
logger.debug("Retrieving diagnostics from recipe")
936+
check.diagnostics(raw_diagnostics)
979937

980938
diagnostics = {}
981939

esmvalcore/preprocessor/_io.py

+2-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import logging
44
import os
55
import shutil
6-
from collections import OrderedDict
76
from itertools import groupby
87
from warnings import catch_warnings, filterwarnings
98

@@ -300,19 +299,6 @@ def cleanup(files, remove=None):
300299
return files
301300

302301

303-
def _ordered_safe_dump(data, stream):
304-
"""Write data containing OrderedDicts to yaml file."""
305-
class _OrderedDumper(yaml.SafeDumper):
306-
pass
307-
308-
def _dict_representer(dumper, data):
309-
return dumper.represent_mapping(
310-
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, data.items())
311-
312-
_OrderedDumper.add_representer(OrderedDict, _dict_representer)
313-
return yaml.dump(data, stream, _OrderedDumper)
314-
315-
316302
def write_metadata(products, write_ncl=False):
317303
"""Write product metadata to file."""
318304
output_files = []
@@ -325,7 +311,7 @@ def write_metadata(products, write_ncl=False):
325311
p.attributes.get('dataset', ''),
326312
),
327313
)
328-
metadata = OrderedDict()
314+
metadata = {}
329315
for product in sorted_products:
330316
if isinstance(product.attributes.get('exp'), (list, tuple)):
331317
product.attributes = dict(product.attributes)
@@ -337,7 +323,7 @@ def write_metadata(products, write_ncl=False):
337323
output_filename = os.path.join(output_dir, 'metadata.yml')
338324
output_files.append(output_filename)
339325
with open(output_filename, 'w') as file:
340-
_ordered_safe_dump(metadata, file)
326+
yaml.safe_dump(metadata, file)
341327
if write_ncl:
342328
output_files.append(_write_ncl_metadata(output_dir, metadata))
343329

tests/unit/preprocessor/_weighting/test_weighting_landsea_fraction.py

+21-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"""Unit tests for :mod:`esmvalcore.preprocessor._weighting`."""
2-
from collections import OrderedDict
32
from unittest import mock
43

54
import iris
@@ -31,27 +30,27 @@
3130
)
3231
FRAC_SFTLF = np.array([0.1, 0.0, 1.0])
3332
FRAC_SFTOF = np.array([0.0, 1.0, 0.5, 0.3])
34-
EMPTY_FX_FILES = OrderedDict([
35-
('sftlf', []),
36-
('sftof', []),
37-
])
38-
L_FX_FILES = OrderedDict([
39-
('sftlf', 'not/a/real/path'),
40-
('sftof', []),
41-
])
42-
O_FX_FILES = OrderedDict([
43-
('sftlf', []),
44-
('sftof', 'not/a/real/path'),
45-
])
46-
FX_FILES = OrderedDict([
47-
('sftlf', 'not/a/real/path'),
48-
('sftof', 'i/was/mocked'),
49-
])
50-
WRONG_FX_FILES = OrderedDict([
51-
('wrong', 'test'),
52-
('sftlf', 'not/a/real/path'),
53-
('sftof', 'i/was/mocked'),
54-
])
33+
EMPTY_FX_FILES = {
34+
'sftlf': [],
35+
'sftof': [],
36+
}
37+
L_FX_FILES = {
38+
'sftlf': 'not/a/real/path',
39+
'sftof': [],
40+
}
41+
O_FX_FILES = {
42+
'sftlf': [],
43+
'sftof': 'not/a/real/path',
44+
}
45+
FX_FILES = {
46+
'sftlf': 'not/a/real/path',
47+
'sftof': 'i/was/mocked',
48+
}
49+
WRONG_FX_FILES = {
50+
'wrong': 'test',
51+
'sftlf': 'not/a/real/path',
52+
'sftof': 'i/was/mocked',
53+
}
5554

5655
LAND_FRACTION = [
5756
(CUBE_3, {}, [], None, ["No fx files given"]),

0 commit comments

Comments
 (0)