From 63c479d58bb69dcc8b06b187436ac19474972bfe Mon Sep 17 00:00:00 2001 From: Tony Hallam Date: Sat, 14 Sep 2019 22:59:15 +0100 Subject: [PATCH] Added try except ImportError for collections.abc which will require a change from Python 3.8. Style follows similar usage elsewhere in deep. Also easy to drop try/except when Python 2.7 may not be supported in the future. --- deap/base.py | 6 +++++- deap/benchmarks/movingpeaks.py | 6 +++++- deap/tools/constraint.py | 6 +++++- deap/tools/crossover.py | 6 +++++- deap/tools/mutation.py | 5 ++++- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/deap/base.py b/deap/base.py index cc78234e2..b57b9663b 100644 --- a/deap/base.py +++ b/deap/base.py @@ -20,7 +20,11 @@ class used as base class, for the fitness member of any individual. """ import sys -from collections import Sequence +try: + from collections.abc import Sequence +except ImportError: + from collections import Sequence + from copy import deepcopy from functools import partial from operator import mul, truediv diff --git a/deap/benchmarks/movingpeaks.py b/deap/benchmarks/movingpeaks.py index cf4b203f3..911d30466 100644 --- a/deap/benchmarks/movingpeaks.py +++ b/deap/benchmarks/movingpeaks.py @@ -24,7 +24,11 @@ import math import itertools import random -from collections import Sequence + +try: + from collections.abc import Sequence +except: + from colections import Sequence def cone(individual, position, height, width): """The cone peak function to be used with scenario 2 and 3. diff --git a/deap/tools/constraint.py b/deap/tools/constraint.py index 22fd7cb3a..5bff220fd 100644 --- a/deap/tools/constraint.py +++ b/deap/tools/constraint.py @@ -1,7 +1,11 @@ from functools import wraps from itertools import repeat -from collections import Sequence + +try: + from collections.abc import Sequence +except ImportError: + from collections import Sequence class DeltaPenalty(object): """This decorator returns penalized fitness for invalid individuals and the diff --git a/deap/tools/crossover.py b/deap/tools/crossover.py index 7f43224b7..4f086fdc2 100644 --- a/deap/tools/crossover.py +++ b/deap/tools/crossover.py @@ -2,7 +2,11 @@ import random import warnings -from collections import Sequence +try: + from collections.abc import Sequence +except ImportError: + from collections import Sequence + from itertools import repeat diff --git a/deap/tools/mutation.py b/deap/tools/mutation.py index d79cf90d9..ac0075d7c 100644 --- a/deap/tools/mutation.py +++ b/deap/tools/mutation.py @@ -3,8 +3,11 @@ import random from itertools import repeat -from collections import Sequence +try: + from collections.abc import Sequence +except ImportError: + from collections import Sequence ###################################### # GA Mutations #