From: Gilles DAVID Date: Tue, 28 Mar 2017 12:42:38 +0000 (+0200) Subject: [PY3] Using Python 3 idioms X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=21807f3462e06b5b43a9ab8a8001355c6f547b1e;p=tools%2Fmedcoupling.git [PY3] Using Python 3 idioms --- diff --git a/doc/user/doxygen/BuildPyExamplesFromCPP.py b/doc/user/doxygen/BuildPyExamplesFromCPP.py index 2f957e10c..fbc36f33b 100644 --- a/doc/user/doxygen/BuildPyExamplesFromCPP.py +++ b/doc/user/doxygen/BuildPyExamplesFromCPP.py @@ -66,8 +66,8 @@ def discardPythonFrom(contents): # Usage: BuildPyExamplesFromCPP.py -fCpp=open(sys.argv[1],"r") -cppCont=fCpp.readlines() ; del fCpp +with open(sys.argv[1], "r") as fCpp: + cppCont = fCpp.readlines() pyCont=cppCont[:] pyCont=[elt.replace("medcouplingcppexamples","medcouplingpyexamples") for elt in pyCont] pyCont=Cpp2Python(pyCont) @@ -79,8 +79,8 @@ cppCont=discardPythonFrom(cppCont) # remove Python-only contents from Cpp # Save CPP and PY examples in two separate dox files outFileName=os.path.join(sys.argv[2],os.path.basename(sys.argv[1])) -f=open(os.path.splitext(outFileName)[0]+"CPP.dox","w") -f.writelines(cppCont) ; del f +with open(os.path.splitext(outFileName)[0] + "CPP.dox", "w") as f: + f.writelines(cppCont) -f=open(os.path.splitext(outFileName)[0]+"PY.dox","w") -f.writelines(pyCont) ; del f +with open(os.path.splitext(outFileName)[0] + "PY.dox", "w") as f: + f.writelines(pyCont) diff --git a/src/INTERP_KERNEL/GaussPoints/CleanUpGauss.py b/src/INTERP_KERNEL/GaussPoints/CleanUpGauss.py index a5c18a116..d8b9f9b98 100644 --- a/src/INTERP_KERNEL/GaussPoints/CleanUpGauss.py +++ b/src/INTERP_KERNEL/GaussPoints/CleanUpGauss.py @@ -48,7 +48,7 @@ dim=nbLines/nbPts-2 if dim<1 or dim>3: raise Exception("Ooops invalid dim !") entries=[(i,int(casePat.match(elt).group(1))) for i,elt in entries] -assert(set([elt[1] for elt in entries])==set(range(nbPts))) +assert({elt[1] for elt in entries}==set(range(nbPts))) # partEndEntries=re.compile("[\s]*break[\s]*\;[\s]*$") zePat=re.compile("[\s]+coords\[([\d]+)\][\s]*=[\s]*([\-]?[\d]+[\.]?[\d]*)[\s]*\;[\s]*$") diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest1.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest1.py index b96d1e767..324dce52c 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest1.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest1.py @@ -2701,8 +2701,7 @@ class MEDCouplingBasicsTest1(unittest.TestCase): types=mesh.getAllGeoTypes(); types.sort() self.assertEqual(5,len(types)); - expected1=[NORM_POLYGON, NORM_TRI3, NORM_QUAD4, NORM_TRI6, NORM_QUAD8] - expected1.sort() + expected1=sorted([NORM_POLYGON, NORM_TRI3, NORM_QUAD4, NORM_TRI6, NORM_QUAD8]) self.assertEqual(expected1,types); self.assertTrue(mesh.isPresenceOfQuadratic()); self.assertEqual(62,mesh.getNodalConnectivityArrayLen()); diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py index 4bb720eef..6e6183379 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py @@ -601,7 +601,7 @@ class MEDCouplingBasicsTest5(unittest.TestCase): m=MEDCoupling1SGTUMesh("m",NORM_QUAD4) mem_m=m.getHeapMemorySize() m.allocateCells(5) - self.assertIn(m.getHeapMemorySize()-mem_m,range(5*4*4,5*4*4+32)) + self.assertIn(m.getHeapMemorySize()-mem_m,list(range(5*4*4,5*4*4+32))) self.assertEqual(m.getNodalConnectivity().getNbOfElemAllocated(),20) m.setCoords(um.getCoords()) m.insertNextCell([1,0,6,7]) @@ -3931,7 +3931,7 @@ class MEDCouplingBasicsTest5(unittest.TestCase): st0=d.repr() ; st1=str(d) ; st2=d.reprNotTooLong() self.assertNotEqual(st0,st1) # 1001 tuples ( > 1000) -> str(d)==d.reprNotTooLong() self.assertEqual(st1,st2) - self.assertIn(len(st2),range(0,1000)) # no more than 1000 characters + self.assertIn(len(st2),list(range(0,1000))) # no more than 1000 characters ## Now for DataArrayInt d=DataArrayInt(2000) ; d.iota() ; d.rearrange(2) st0=d.repr() ; st1=str(d) ; st2=d.reprNotTooLong() @@ -3942,7 +3942,7 @@ class MEDCouplingBasicsTest5(unittest.TestCase): st0=d.repr() ; st1=str(d) ; st2=d.reprNotTooLong() self.assertNotEqual(st0,st1) # 1001 tuples ( > 1000) -> str(d)==d.reprNotTooLong() self.assertEqual(st1,st2) - self.assertIn(len(st2),range(0,1000)) # no more than 1000 characters + self.assertIn(len(st2),list(range(0,1000))) # no more than 1000 characters pass def testExtrudedMeshWithoutZipCoords1(self): diff --git a/src/MEDLoader/Swig/CaseIO.py b/src/MEDLoader/Swig/CaseIO.py index 9c0467f2a..fa61441de 100644 --- a/src/MEDLoader/Swig/CaseIO.py +++ b/src/MEDLoader/Swig/CaseIO.py @@ -25,7 +25,7 @@ class CaseIO: dictMCTyp={NORM_HEXA8:"hexa8",NORM_POLYHED:"nfaced",NORM_QUAD4:"quad4",NORM_POLYGON:"nsided",NORM_POINT1:"point",NORM_SEG2:"bar2",NORM_SEG3:"bar3",NORM_TRI3:"tria3",NORM_TRI6:"tria6",NORM_QUAD8:"quad8",NORM_TETRA4:"tetra4",NORM_TETRA10:"tetra10",NORM_PYRA5:"pyramid5",NORM_PYRA13:"pyramid13",NORM_PENTA6:"penta6",NORM_PENTA15:"penta15",NORM_HEXA20:"hexa20"} discSpatial={ON_CELLS:"element",ON_NODES:"node"} dictCompo={1:"scalar",3:"vector",6:"tensor",9:"tensor9"} - dictMCTyp2=dict((v,k) for k,v in dictMCTyp.items()) - discSpatial2=dict((v,k) for k,v in discSpatial.items()) - dictCompo2=dict((v,k) for k,v in dictCompo.items()) + dictMCTyp2 = {v:k for k, v in dictMCTyp.items()} + discSpatial2 = {v:k for k, v in discSpatial.items()} + dictCompo2 = {v:k for k, v in dictCompo.items()} pass diff --git a/src/MEDLoader/Swig/MEDLoaderCouplingTrainingSession.py b/src/MEDLoader/Swig/MEDLoaderCouplingTrainingSession.py index 135c1b9cd..c620a87f4 100644 --- a/src/MEDLoader/Swig/MEDLoaderCouplingTrainingSession.py +++ b/src/MEDLoader/Swig/MEDLoaderCouplingTrainingSession.py @@ -106,8 +106,7 @@ mesh3D.getCoords()[:]*=100. mesh3D.getCoords().setInfoOnComponents(["X [cm]","Y [cm]","Z [cm]"]) # zLev=mesh3D.getCoords()[:,2] -zLev=zLev.getDifferentValues(1e-12) -zLev.sort() +zLev=sorted(zLev.getDifferentValues(1e-12)) # tmp,cellIdsSol1=mesh3D.buildSlice3D([0.,0.,(zLev[1]+zLev[2])/2],[0.,0.,1.],1e-12) bary=mesh3D.computeCellCenterOfMass() diff --git a/src/MEDLoader/Swig/MEDLoaderTest3.py b/src/MEDLoader/Swig/MEDLoaderTest3.py index d9957a823..b5c71af77 100644 --- a/src/MEDLoader/Swig/MEDLoaderTest3.py +++ b/src/MEDLoader/Swig/MEDLoaderTest3.py @@ -1411,8 +1411,8 @@ class MEDLoaderTest3(unittest.TestCase): self.assertEqual(nNod+2, m1_bis.getNumberOfNodes()) self.assertEqual([6,7], nodesDup.getValues()) self.assertEqual([2.,1., 3.,1.], m2_bis.getCoords()[nNod:].getValues()) - self.assertEqual(set([3,10,11]), set(cells1.getValues())) - self.assertEqual(set([8,9,4,5]), set(cells2.getValues())) + self.assertEqual({3,10,11}, set(cells1.getValues())) + self.assertEqual({8,9,4,5}, set(cells2.getValues())) self.assertEqual([9,11],mfu.getGroupArr(-1,"group").getValues()) self.assertEqual([23,24],mfu.getGroupArr(-1,"group_dup").getValues()) self.assertEqual([0,1],mfu.getGroupArr(-1,"group2").getValues()) @@ -1477,8 +1477,8 @@ class MEDLoaderTest3(unittest.TestCase): self.assertEqual(nNod+1, m2_bis.getNumberOfNodes()) self.assertEqual([3], nodesDup.getValues()) self.assertEqual(m3_bis.getCoords()[3].getValues(), m3_bis.getCoords()[nNod:].getValues()) - self.assertEqual(set([22]), set(cells1.getValues())) - self.assertEqual(set([77]), set(cells2.getValues())) + self.assertEqual({22}, set(cells1.getValues())) + self.assertEqual({77}, set(cells2.getValues())) self.assertEqual([36,74],mfu.getGroupArr(-1,"group").getValues()) self.assertEqual([0,1],mfu.getGroupArr(0,"group_3d").getValues()) self.assertEqual([213],mfu.getGroupArr(-1,"group_dup").getValues()) # here only one cell has been duplicated @@ -2143,30 +2143,30 @@ class MEDLoaderTest3(unittest.TestCase): m=m.buildUnstructured() m.setName("mm") f=m.getMeasureField(False) - self.assertIn(m.getHeapMemorySize(),range(3552-100,3552+100+4*strMulFac)) - self.assertIn(f.getHeapMemorySize(),range(4215-100,4215+100+8*strMulFac)) + self.assertIn(m.getHeapMemorySize(),list(range(3552-100,3552+100+4*strMulFac))) + self.assertIn(f.getHeapMemorySize(),list(range(4215-100,4215+100+8*strMulFac))) # mm=MEDFileUMesh() mm.setMeshAtLevel(0,m) - self.assertIn(mm.getHeapMemorySize(),range(3889-100,4225+100+10*strMulFac)) + self.assertIn(mm.getHeapMemorySize(),list(range(3889-100,4225+100+10*strMulFac))) ff=MEDFileField1TS() ff.setFieldNoProfileSBT(f) - self.assertIn(ff.getHeapMemorySize(),range(771-40,871+21+(4+1)*strMulFac)) + self.assertIn(ff.getHeapMemorySize(),list(range(771-40,871+21+(4+1)*strMulFac))) # fff=MEDFileFieldMultiTS() fff.appendFieldNoProfileSBT(f) - self.assertIn(fff.getHeapMemorySize(),range(815-50,915+30+(6+2)*strMulFac)) + self.assertIn(fff.getHeapMemorySize(),list(range(815-50,915+30+(6+2)*strMulFac))) f.setTime(1.,0,-1) fff.appendFieldNoProfileSBT(f) - self.assertIn(fff.getHeapMemorySize(),range(1594-90,1794+50+(10+1)*strMulFac)) - self.assertIn(fff[0,-1].getHeapMemorySize(),range(771-40,871+20+(4+1)*strMulFac)) + self.assertIn(fff.getHeapMemorySize(),list(range(1594-90,1794+50+(10+1)*strMulFac))) + self.assertIn(fff[0,-1].getHeapMemorySize(),list(range(771-40,871+20+(4+1)*strMulFac))) f2=f[:50] f2.setTime(2.,1,-1) pfl=DataArrayInt.Range(0,50,1) ; pfl.setName("pfl") fff.appendFieldProfile(f2,mm,0,pfl) - self.assertIn(fff.getHeapMemorySize(),range(2348-130,2608+100+(10+2)*strMulFac)) - self.assertIn(fff.getProfile("pfl").getHeapMemorySize(),range(204-10,204+10+2*strMulFac)) - self.assertIn(fff[1,-1].getHeapMemorySize(),range(738-50,838+30+4*strMulFac)) + self.assertIn(fff.getHeapMemorySize(),list(range(2348-130,2608+100+(10+2)*strMulFac))) + self.assertIn(fff.getProfile("pfl").getHeapMemorySize(),list(range(204-10,204+10+2*strMulFac))) + self.assertIn(fff[1,-1].getHeapMemorySize(),list(range(738-50,838+30+4*strMulFac))) pass def testCurveLinearMesh1(self): @@ -3078,7 +3078,7 @@ class MEDLoaderTest3(unittest.TestCase): self.assertTrue(not ff0.getUndergroundDataArray().isAllocated()) self.assertEqual(ff0.getUndergroundDataArray().getInfoOnComponents(),['X [km]','YY [mm]']) heap_memory_ref=ff0.getHeapMemorySize() - self.assertIn(heap_memory_ref,range(182,465+2*strMulFac)) + self.assertIn(heap_memory_ref,list(range(182,465+2*strMulFac))) ff0.loadArrays() ## arr=DataArrayDouble(140) ; arr.iota() ; arr.rearrange(2) self.assertTrue(ff0.getUndergroundDataArray().isEqualWithoutConsideringStr(arr,1e-14)) @@ -3087,7 +3087,7 @@ class MEDLoaderTest3(unittest.TestCase): ff0=MEDFileField1TS(fname,"FieldCellPfl",False) self.assertEqual(ff0.getUndergroundDataArray().getInfoOnComponents(),["XX [pm]","YYY [hm]"]) heap_memory_ref=ff0.getHeapMemorySize() - self.assertIn(heap_memory_ref,range(350,520+6*strMulFac)) + self.assertIn(heap_memory_ref,list(range(350,520+6*strMulFac))) ff0.loadArrays() ## arr=DataArrayDouble(100) ; arr.iota() ; arr.rearrange(2) self.assertTrue(ff0.getUndergroundDataArray().isEqualWithoutConsideringStr(arr,1e-14)) @@ -3105,7 +3105,7 @@ class MEDLoaderTest3(unittest.TestCase): self.assertEqual(ff0.getUndergroundDataArray().getIJ(30,1),5.5) self.assertTrue(not ff0.getUndergroundDataArray().isEqualWithoutConsideringStr(arr,1e-14)) heap_memory_ref=ff0.getHeapMemorySize() - self.assertIn(heap_memory_ref,range(1100,1384+2*strMulFac)) + self.assertIn(heap_memory_ref,list(range(1100,1384+2*strMulFac))) ff0.unloadArrays() hmd=ff0.getHeapMemorySize()-heap_memory_ref self.assertEqual(hmd,-800) # -50*8*2 @@ -3114,7 +3114,7 @@ class MEDLoaderTest3(unittest.TestCase): # ff0=MEDFileField1TS(fname,"FieldCellPfl",-1,-1,False) heap_memory_ref=ff0.getHeapMemorySize() - self.assertIn(heap_memory_ref,range(299,520+6*strMulFac)) + self.assertIn(heap_memory_ref,list(range(299,520+6*strMulFac))) ff0.loadArrays() ## self.assertTrue(ff0.getUndergroundDataArray().isEqualWithoutConsideringStr(arr,1e-14)) self.assertEqual(ff0.getHeapMemorySize()-heap_memory_ref,50*8*2) @@ -3131,14 +3131,14 @@ class MEDLoaderTest3(unittest.TestCase): # ff0=MEDFileAnyTypeFieldMultiTS.New(fname,fieldName,False) heap_memory_ref=ff0.getHeapMemorySize() - self.assertIn(heap_memory_ref,range(5536,8212+(80+26+1)*strMulFac)) + self.assertIn(heap_memory_ref,list(range(5536,8212+(80+26+1)*strMulFac))) ff0.loadArrays() self.assertEqual(ff0.getHeapMemorySize()-heap_memory_ref,20*70*8*2) del ff0 # ffs=MEDFileFields(fname,False) heap_memory_ref=ffs.getHeapMemorySize() - self.assertIn(heap_memory_ref,range(5335,9031+(80+50+len(ffs))*strMulFac)) + self.assertIn(heap_memory_ref,list(range(5335,9031+(80+50+len(ffs))*strMulFac))) ffs.loadArrays() self.assertEqual(ffs.getHeapMemorySize()-heap_memory_ref,20*70*8*2+70*8*2+50*8*2) pass @@ -5618,8 +5618,8 @@ class MEDLoaderTest3(unittest.TestCase): tester.assertTrue(mm.getFamilyFieldAtLevel(-1).isEqual(DataArrayInt([6,7,200,201,202,8,9,10,11,203,204,205,206,207]))) tester.assertTrue(mm.getNumberFieldAtLevel(-1).isEqual(DataArrayInt([16,17,300,301,302,18,19,20,21,303,304,305,306,307]))) refFamIds=[("FAMILLE_ZERO",0),('F1',1),('F2',2),('F3',3),('F4',4),('F5',5),('F6',6),('F7',7),('F8',8),('F9',9),('G1',31),('G10',40),('G11',41),('G2',32),('G3',33),('G4',34),('G5',35),('G6',36),('G7',37),('G8',38),('G9',39),("H1",100)] - tester.assertEqual(set(mm.getFamiliesNames()),set([elt[0] for elt in refFamIds])) - tester.assertEqual(set([mm.getFamilyId(elt) for elt in mm.getFamiliesNames()]),set([elt[1] for elt in refFamIds])) + tester.assertEqual(set(mm.getFamiliesNames()),{elt[0] for elt in refFamIds}) + tester.assertEqual({mm.getFamilyId(elt) for elt in mm.getFamiliesNames()},{elt[1] for elt in refFamIds}) tester.assertEqual(mm.getGroupsNames(),('myGRP','myGRP1','myGRP2')) tester.assertEqual(mm.getAllDistributionOfTypes(),[(NORM_TRI3,5),(NORM_QUAD4,9),(NORM_TETRA4,5),(NORM_PENTA6,4),(NORM_ERROR,9)]) pass diff --git a/src/MEDLoader/Swig/VTKReader.py b/src/MEDLoader/Swig/VTKReader.py index 012ccb575..7fc3c11f3 100644 --- a/src/MEDLoader/Swig/VTKReader.py +++ b/src/MEDLoader/Swig/VTKReader.py @@ -285,11 +285,11 @@ class VTURawReader: def __init__(self,fileName,tim=(0.,0)): msg="The time specified in constructor as 2nd arg should be a tuple containing 2 values 1 float and 1 int !" - if type(tim)!=tuple: + if not isinstance(tim, tuple): raise Exception(msg) if len(tim)!=2: raise Exception(msg) - if type(tim[0])!=float or type(tim[1])!=int: + if not isinstance(tim[0], float) or not isinstance(tim[1], int): raise Exception(msg) self._fileName=fileName self._time=tim