|
1 | | -# (C) British Crown Copyright 2010 - 2018, Met Office |
| 1 | +# (C) British Crown Copyright 2010 - 2019, Met Office |
2 | 2 | # |
3 | 3 | # This file is part of Iris. |
4 | 4 | # |
|
24 | 24 | from six.moves import (filter, input, map, range, zip) # noqa |
25 | 25 | import six |
26 | 26 |
|
27 | | -import collections |
| 27 | +from collections import namedtuple, OrderedDict |
| 28 | +try: # Python 3 |
| 29 | + from collections.abc import (Iterable, |
| 30 | + Container, |
| 31 | + Mapping, |
| 32 | + MutableMapping, |
| 33 | + Iterator) |
| 34 | +except: # Python 2.7 |
| 35 | + from collections import (Iterable, |
| 36 | + Container, |
| 37 | + Mapping, |
| 38 | + MutableMapping, |
| 39 | + Iterator) |
28 | 40 | import copy |
29 | 41 | from copy import deepcopy |
30 | 42 | import datetime |
|
58 | 70 | __all__ = ['Cube', 'CubeList', 'CubeMetadata'] |
59 | 71 |
|
60 | 72 |
|
61 | | -class CubeMetadata(collections.namedtuple('CubeMetadata', |
62 | | - ['standard_name', |
63 | | - 'long_name', |
64 | | - 'var_name', |
65 | | - 'units', |
66 | | - 'attributes', |
67 | | - 'cell_methods'])): |
| 73 | +class CubeMetadata(namedtuple('CubeMetadata', |
| 74 | + ['standard_name', |
| 75 | + 'long_name', |
| 76 | + 'var_name', |
| 77 | + 'units', |
| 78 | + 'attributes', |
| 79 | + 'cell_methods'])): |
68 | 80 | """ |
69 | 81 | Represents the phenomenon metadata for a single :class:`Cube`. |
70 | 82 |
|
@@ -495,7 +507,7 @@ def concatenate_cube(self, check_aux_coords=True): |
495 | 507 | raise ValueError("can't concatenate an empty CubeList") |
496 | 508 |
|
497 | 509 | names = [cube.metadata.name() for cube in self] |
498 | | - unique_names = list(collections.OrderedDict.fromkeys(names)) |
| 510 | + unique_names = list(OrderedDict.fromkeys(names)) |
499 | 511 | if len(unique_names) == 1: |
500 | 512 | res = iris._concatenate.concatenate( |
501 | 513 | self, error_on_mismatch=True, |
@@ -629,7 +641,7 @@ def _is_single_item(testee): |
629 | 641 |
|
630 | 642 | """ |
631 | 643 | return (isinstance(testee, six.string_types) or |
632 | | - not isinstance(testee, collections.Iterable)) |
| 644 | + not isinstance(testee, Iterable)) |
633 | 645 |
|
634 | 646 |
|
635 | 647 | class Cube(CFVariableMixin): |
@@ -952,7 +964,7 @@ def _check_multi_dim_metadata(self, metadata, data_dims): |
952 | 964 | # Convert to a tuple of integers |
953 | 965 | if data_dims is None: |
954 | 966 | data_dims = tuple() |
955 | | - elif isinstance(data_dims, collections.Container): |
| 967 | + elif isinstance(data_dims, Container): |
956 | 968 | data_dims = tuple(int(d) for d in data_dims) |
957 | 969 | else: |
958 | 970 | data_dims = (int(data_dims),) |
@@ -1064,7 +1076,7 @@ def _add_unique_dim_coord(self, dim_coord, data_dim): |
1064 | 1076 | raise ValueError('The dim_coord may not be an AuxCoord instance.') |
1065 | 1077 |
|
1066 | 1078 | # Convert data_dim to a single integer |
1067 | | - if isinstance(data_dim, collections.Container): |
| 1079 | + if isinstance(data_dim, Container): |
1068 | 1080 | if len(data_dim) != 1: |
1069 | 1081 | raise ValueError('The supplied data dimension must be a' |
1070 | 1082 | ' single number.') |
@@ -1366,7 +1378,7 @@ def coords(self, name_or_coord=None, standard_name=None, |
1366 | 1378 | if guess_axis(coord_) == axis] |
1367 | 1379 |
|
1368 | 1380 | if attributes is not None: |
1369 | | - if not isinstance(attributes, collections.Mapping): |
| 1381 | + if not isinstance(attributes, Mapping): |
1370 | 1382 | msg = 'The attributes keyword was expecting a dictionary ' \ |
1371 | 1383 | 'type, but got a %s instead.' % type(attributes) |
1372 | 1384 | raise ValueError(msg) |
@@ -1396,7 +1408,7 @@ def attr_filter(coord_): |
1396 | 1408 | self.coord_dims(coord_)] |
1397 | 1409 |
|
1398 | 1410 | if dimensions is not None: |
1399 | | - if not isinstance(dimensions, collections.Container): |
| 1411 | + if not isinstance(dimensions, Container): |
1400 | 1412 | dimensions = [dimensions] |
1401 | 1413 | dimensions = tuple(dimensions) |
1402 | 1414 | coords_and_factories = [coord_ for coord_ in coords_and_factories |
@@ -3185,7 +3197,7 @@ def collapsed(self, coords, aggregator, **kwargs): |
3185 | 3197 | for coord in coords] |
3186 | 3198 |
|
3187 | 3199 | # Remove duplicate dimensions. |
3188 | | - new_dims = collections.OrderedDict.fromkeys( |
| 3200 | + new_dims = OrderedDict.fromkeys( |
3189 | 3201 | d for dim in dims_to_collapse for d in dim) |
3190 | 3202 | # Reverse the dimensions so the order can be maintained when |
3191 | 3203 | # reshaping the data. |
@@ -3753,7 +3765,7 @@ def regrid(self, grid, scheme): |
3753 | 3765 | return regridder(self) |
3754 | 3766 |
|
3755 | 3767 |
|
3756 | | -class ClassDict(collections.MutableMapping, object): |
| 3768 | +class ClassDict(MutableMapping, object): |
3757 | 3769 | """ |
3758 | 3770 | A mapping that stores objects keyed on their superclasses and their names. |
3759 | 3771 |
|
@@ -3839,7 +3851,7 @@ def sorted_axes(axes): |
3839 | 3851 |
|
3840 | 3852 |
|
3841 | 3853 | # See Cube.slice() for the definition/context. |
3842 | | -class _SliceIterator(collections.Iterator): |
| 3854 | +class _SliceIterator(Iterator): |
3843 | 3855 | def __init__(self, cube, dims_index, requested_dims, ordered): |
3844 | 3856 | self._cube = cube |
3845 | 3857 |
|
|
0 commit comments