Merge pull request #62 from hellock/collections-abc

Use collections.abc instead of collections
This commit is contained in:
Kai Chen 2019-04-21 19:33:54 -07:00 committed by GitHub
commit d99c6f8ddf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 9 deletions

View File

@ -1,11 +1,11 @@
import os.path as osp import os.path as osp
import sys import sys
from argparse import ArgumentParser from argparse import ArgumentParser
from collections import Iterable
from importlib import import_module from importlib import import_module
from addict import Dict from addict import Dict
from .misc import collections_abc
from .path import check_file_exist from .path import check_file_exist
@ -39,7 +39,7 @@ def add_args(parser, cfg, prefix=''):
parser.add_argument('--' + prefix + k, action='store_true') parser.add_argument('--' + prefix + k, action='store_true')
elif isinstance(v, dict): elif isinstance(v, dict):
add_args(parser, v, k + '.') add_args(parser, v, k + '.')
elif isinstance(v, Iterable): elif isinstance(v, collections_abc.Iterable):
parser.add_argument('--' + prefix + k, type=type(v[0]), nargs='+') parser.add_argument('--' + prefix + k, type=type(v[0]), nargs='+')
else: else:
print('connot parse key {} of type {}'.format(prefix + k, type(v))) print('connot parse key {} of type {}'.format(prefix + k, type(v)))

View File

@ -3,6 +3,12 @@ import functools
import itertools import itertools
import subprocess import subprocess
from importlib import import_module from importlib import import_module
# ABCs from collections will be deprecated in python 3.8+,
# while collections.abc is not available in python 2.7
try:
import collections.abc as collections_abc
except ImportError:
import collections as collections_abc
import six import six
@ -24,7 +30,7 @@ def iter_cast(inputs, dst_type, return_type=None):
Returns: Returns:
iterator or specified type: The converted object. iterator or specified type: The converted object.
""" """
if not isinstance(inputs, collections.Iterable): if not isinstance(inputs, collections_abc.Iterable):
raise TypeError('inputs must be an iterable object') raise TypeError('inputs must be an iterable object')
if not isinstance(dst_type, type): if not isinstance(dst_type, type):
raise TypeError('"dst_type" must be a valid type') raise TypeError('"dst_type" must be a valid type')
@ -65,7 +71,7 @@ def is_seq_of(seq, expected_type, seq_type=None):
bool: Whether the sequence is valid. bool: Whether the sequence is valid.
""" """
if seq_type is None: if seq_type is None:
exp_seq_type = collections.Sequence exp_seq_type = collections_abc.Sequence
else: else:
assert isinstance(seq_type, type) assert isinstance(seq_type, type)
exp_seq_type = seq_type exp_seq_type = seq_type

View File

@ -1,7 +1,7 @@
import collections
import sys import sys
from multiprocessing import Pool from multiprocessing import Pool
from .misc import collections_abc
from .timer import Timer from .timer import Timer
@ -76,11 +76,11 @@ def track_progress(func, tasks, bar_width=50, **kwargs):
""" """
if isinstance(tasks, tuple): if isinstance(tasks, tuple):
assert len(tasks) == 2 assert len(tasks) == 2
assert isinstance(tasks[0], collections.Iterable) assert isinstance(tasks[0], collections_abc.Iterable)
assert isinstance(tasks[1], int) assert isinstance(tasks[1], int)
task_num = tasks[1] task_num = tasks[1]
tasks = tasks[0] tasks = tasks[0]
elif isinstance(tasks, collections.Iterable): elif isinstance(tasks, collections_abc.Iterable):
task_num = len(tasks) task_num = len(tasks)
else: else:
raise TypeError( raise TypeError(
@ -141,11 +141,11 @@ def track_parallel_progress(func,
""" """
if isinstance(tasks, tuple): if isinstance(tasks, tuple):
assert len(tasks) == 2 assert len(tasks) == 2
assert isinstance(tasks[0], collections.Iterable) assert isinstance(tasks[0], collections_abc.Iterable)
assert isinstance(tasks[1], int) assert isinstance(tasks[1], int)
task_num = tasks[1] task_num = tasks[1]
tasks = tasks[0] tasks = tasks[0]
elif isinstance(tasks, collections.Iterable): elif isinstance(tasks, collections_abc.Iterable):
task_num = len(tasks) task_num = len(tasks)
else: else:
raise TypeError( raise TypeError(