From 651fca0d558ca360ec1e1d99b931ff5f4b24c58a Mon Sep 17 00:00:00 2001 From: Jean-Philippe ARGAUD Date: Sun, 19 Mar 2017 20:38:29 +0100 Subject: [PATCH] Internal structures modification for Python 3 support --- src/daComposant/daCore/BasicObjects.py | 2 +- src/daComposant/daCore/Persistence.py | 51 ++++++++++++------- src/daComposant/daCore/Templates.py | 2 +- .../daNumerics/ApproximatedDerivatives.py | 2 +- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/daComposant/daCore/BasicObjects.py b/src/daComposant/daCore/BasicObjects.py index 6d99271..917848a 100644 --- a/src/daComposant/daCore/BasicObjects.py +++ b/src/daComposant/daCore/BasicObjects.py @@ -463,7 +463,7 @@ class Algorithm(object): dictionnaire des paramètres requis. """ if noDetails: - ks = self.__required_parameters.keys() + ks = list(self.__required_parameters.keys()) ks.sort() return ks else: diff --git a/src/daComposant/daCore/Persistence.py b/src/daComposant/daCore/Persistence.py index b9c6577..3ff81f5 100644 --- a/src/daComposant/daCore/Persistence.py +++ b/src/daComposant/daCore/Persistence.py @@ -27,7 +27,7 @@ __author__ = "Jean-Philippe ARGAUD" __all__ = [] -import numpy, copy +import sys, numpy, copy from daCore.PlatformInfo import PathManagement ; PathManagement() @@ -238,7 +238,7 @@ class Persistence(object): allTags = {} for index in __indexOfFilteredItems: allTags.update( self.__tags[index] ) - allKeys = allTags.keys() + allKeys = list(allTags.keys()) allKeys.sort() return allKeys @@ -594,7 +594,7 @@ class Persistence(object): self.__g('set ylabel "'+str(ylabel).encode('ascii','replace')+'"') # # Tracé du ou des vecteurs demandés - indexes = range(len(self.__values)) + indexes = list(range(len(self.__values))) self.__g.plot( self.__gnuplot.Data( Steps, self.__values[indexes.pop(0)], title=ltitle+" (pas 0)" ) ) for index in indexes: self.__g.replot( self.__gnuplot.Data( Steps, self.__values[index], title=ltitle+" (pas %i)"%index ) ) @@ -610,20 +610,30 @@ class Persistence(object): Association à la variable d'un triplet définissant un observer Le Scheduler attendu est une fréquence, une simple liste d'index ou un - xrange des index. + range des index. """ # # Vérification du Scheduler # ------------------------- maxiter = int( 1e9 ) - if isinstance(Scheduler,int): # Considéré comme une fréquence à partir de 0 - Schedulers = xrange( 0, maxiter, int(Scheduler) ) - elif isinstance(Scheduler,xrange): # Considéré comme un itérateur - Schedulers = Scheduler - elif isinstance(Scheduler,list): # Considéré comme des index explicites - Schedulers = [long(i) for i in Scheduler] # map( long, Scheduler ) - else: # Dans tous les autres cas, activé par défaut - Schedulers = xrange( 0, maxiter ) + if sys.version.split()[0] < '3': + if isinstance(Scheduler,int): # Considéré comme une fréquence à partir de 0 + Schedulers = xrange( 0, maxiter, int(Scheduler) ) + elif isinstance(Scheduler,xrange): # Considéré comme un itérateur + Schedulers = Scheduler + elif isinstance(Scheduler,(list,tuple)): # Considéré comme des index explicites + Schedulers = [long(i) for i in Scheduler] # map( long, Scheduler ) + else: # Dans tous les autres cas, activé par défaut + Schedulers = xrange( 0, maxiter ) + else: + if isinstance(Scheduler,int): # Considéré comme une fréquence à partir de 0 + Schedulers = range( 0, maxiter, int(Scheduler) ) + elif sys.version.split()[0] > '3' and isinstance(Scheduler,range): # Considéré comme un itérateur + Schedulers = Scheduler + elif isinstance(Scheduler,(list,tuple)): # Considéré comme des index explicites + Schedulers = [int(i) for i in Scheduler] # map( int, Scheduler ) + else: # Dans tous les autres cas, activé par défaut + Schedulers = range( 0, maxiter ) # # Stockage interne de l'observer dans la variable # ----------------------------------------------- @@ -647,7 +657,7 @@ class Persistence(object): index_to_remove = [] for [hf, hp, hs] in self.__dataobservers: i = i + 1 - if name is hf.func_name: index_to_remove.append( i ) + if name is hf.__name__: index_to_remove.append( i ) index_to_remove.reverse() for i in index_to_remove: self.__dataobservers.pop( i ) @@ -838,6 +848,7 @@ class CompositePersistence(object): finally: pass objs = usedObjs + objs = list(objs) objs.sort() return objs @@ -858,7 +869,10 @@ class CompositePersistence(object): else: filename = os.path.abspath( filename ) # - import cPickle + if sys.version.split()[0] < '3': + import cPickle as lPickle + else: + import pickle as lPickle if mode == "pickle": if compress == "gzip": import gzip @@ -868,7 +882,7 @@ class CompositePersistence(object): output = bz2.BZ2File( filename, 'wb') else: output = open( filename, 'wb') - cPickle.dump(self, output) + lPickle.dump(self, output) output.close() else: raise ValueError("Save mode '%s' unknown. Choose another one."%mode) @@ -885,7 +899,10 @@ class CompositePersistence(object): else: filename = os.path.abspath( filename ) # - import cPickle + if sys.version.split()[0] < '3': + import cPickle as lPickle + else: + import pickle as lPickle if mode == "pickle": if compress == "gzip": import gzip @@ -895,7 +912,7 @@ class CompositePersistence(object): pkl_file = bz2.BZ2File( filename, 'rb') else: pkl_file = open(filename, 'rb') - output = cPickle.load(pkl_file) + output = lPickle.load(pkl_file) for k in output.keys(): self[k] = output[k] else: diff --git a/src/daComposant/daCore/Templates.py b/src/daComposant/daCore/Templates.py index c7ab6b1..a6d1ec4 100644 --- a/src/daComposant/daCore/Templates.py +++ b/src/daComposant/daCore/Templates.py @@ -56,7 +56,7 @@ class TemplateStorage(object): def keys(self): "D.keys() -> list of D's keys" - __keys = self.__values.keys() + __keys = list(self.__values.keys()) __keys.sort() return __keys diff --git a/src/daComposant/daNumerics/ApproximatedDerivatives.py b/src/daComposant/daNumerics/ApproximatedDerivatives.py index 4cc812d..cd6581b 100644 --- a/src/daComposant/daNumerics/ApproximatedDerivatives.py +++ b/src/daComposant/daNumerics/ApproximatedDerivatives.py @@ -139,7 +139,7 @@ class FDApproximation(object): # --------------------------------------------------------- def __doublon__(self, e, l, n, v=None): __ac, __iac = False, -1 - for i in xrange(len(l)-1,-1,-1): + for i in range(len(l)-1,-1,-1): if numpy.linalg.norm(e - l[i]) < self.__tolerBP * n[i]: __ac, __iac = True, i if v is not None: logging.debug("FDA Cas%s déja calculé, récupération du doublon %i"%(v,__iac)) -- 2.39.2