From c5d0c7e52ea63eb1e0d15b04897cd15171b6b2af Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Wed, 3 Jun 2020 10:43:07 +0200 Subject: [PATCH] easier to read, more flexible --- python/damask/__init__.py | 4 ++-- python/damask/_result.py | 10 ++++------ python/damask/_vtk.py | 12 ++++++------ 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/python/damask/__init__.py b/python/damask/__init__.py index 0f343a581..6d65f7cd1 100644 --- a/python/damask/__init__.py +++ b/python/damask/__init__.py @@ -1,9 +1,9 @@ """Tools for pre and post processing of DAMASK simulations.""" -import os as _os +from pathlib import Path as _Path import re as _re name = 'damask' -with open(_os.path.join(_os.path.dirname(__file__),'VERSION')) as _f: +with open(_Path(__file__).parent/_Path('VERSION')) as _f: version = _re.sub(r'^v','',_f.readline().strip()) # make classes directly accessible as damask.Class diff --git a/python/damask/_result.py b/python/damask/_result.py index 73a038c4c..b6ac3782e 100644 --- a/python/damask/_result.py +++ b/python/damask/_result.py @@ -6,6 +6,7 @@ import os import datetime import xml.etree.ElementTree as ET import xml.dom.minidom +from pathlib import Path from functools import partial import h5py @@ -88,7 +89,7 @@ class Result: 'con_physics': self.con_physics, 'mat_physics': self.mat_physics } - self.fname = os.path.abspath(fname) + self.fname = Path(fname).absolute() self._allow_overwrite = False @@ -1163,7 +1164,7 @@ class Result: 'Dimensions': '{} {} {} {}'.format(*self.grid,np.prod(shape))} data_items[-1].text='{}:{}'.format(os.path.split(self.fname)[1],name) - with open(os.path.splitext(self.fname)[0]+'.xdmf','w') as f: + with open(self.fname.with_suffix('.xdmf').name,'w') as f: f.write(xml.dom.minidom.parseString(ET.tostring(xdmf).decode()).toprettyxml()) @@ -1239,7 +1240,4 @@ class Result: u = self.read_dataset(self.get_dataset_location('u_n' if mode.lower() == 'cell' else 'u_p')) v.add(u,'u') - file_out = '{}_inc{}'.format(os.path.splitext(os.path.basename(self.fname))[0], - inc[3:].zfill(N_digits)) - - v.write(file_out) + v.write('{}_inc{}'.format(self.fname.stem,inc[3:].zfill(N_digits))) diff --git a/python/damask/_vtk.py b/python/damask/_vtk.py index 58b43dc1f..72e224bda 100644 --- a/python/damask/_vtk.py +++ b/python/damask/_vtk.py @@ -1,4 +1,4 @@ -import os +from pathlib import Path import pandas as pd import numpy as np @@ -126,8 +126,8 @@ class VTK: vtkUnstructuredGrid, and vtkPolyData. """ - ext = os.path.splitext(fname)[1] - if ext == '.vtk': + ext = Path(fname).suffix + if ext == '.vtk' or dataset_type: reader = vtk.vtkGenericDataObjectReader() reader.SetFileName(fname) reader.Update() @@ -176,10 +176,10 @@ class VTK: writer = vtk.vtkXMLPolyDataWriter() default_ext = writer.GetDefaultFileExtension() - name, ext = os.path.splitext(fname) + ext = Path(fname).suffix if ext and ext != '.'+default_ext: - raise ValueError('Given extension {} is not .{}'.format(ext,default_ext)) - writer.SetFileName('{}.{}'.format(name,default_ext)) + raise ValueError('Given extension {} does not match default .{}'.format(ext,default_ext)) + writer.SetFileName(str(Path(fname).with_suffix('.'+default_ext))) writer.SetCompressorTypeToZLib() writer.SetDataModeToBinary() writer.SetInputData(self.geom)