Skip to content

Commit 2254040

Browse files
committed
Change filename -> __filename & add query function.
Change filename to private member and add corresponding query function.
1 parent 8b50349 commit 2254040

File tree

5 files changed

+41
-28
lines changed

5 files changed

+41
-28
lines changed

vaspy/atomco.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Provide coordinate file class which do operations on these files.
55
===================================================================
66
Written by PytLab <shaozhengjiang@gmail.com>, November 2014
7-
Updated by PytLab <shaozhengjiang@gmail.com>, November 2015
7+
Updated by PytLab <shaozhengjiang@gmail.com>, May 2016
88
99
==============================================================
1010
@@ -18,13 +18,13 @@
1818
class AtomCo(VasPy):
1919
"Base class to be inherited by atomco classes."
2020
def __init__(self, filename):
21-
VasPy.__init__(self, filename)
21+
super(self.__class__, self).__init__(filename)
2222

2323
def __repr__(self):
2424
if hasattr(self, 'get_content'):
2525
return self.get_content()
2626
else:
27-
return self.filename
27+
return self.filename()
2828

2929
def __str__(self):
3030
return self.__repr__()
@@ -177,13 +177,13 @@ class XyzFile(AtomCo):
177177
============ =======================================================
178178
"""
179179
def __init__(self, filename):
180-
AtomCo.__init__(self, filename)
180+
super(self.__class__, self).__init__(filename)
181181
self.load()
182182
self.verify()
183183

184184
def load(self):
185185
"加载文件内容"
186-
with open(self.filename, 'r') as f:
186+
with open(self.filename(), 'r') as f:
187187
content_list = f.readlines()
188188
ntot = int(content_list[0].strip()) # total atom number
189189
step = int(str2list(content_list[1])[-1]) # iter step number
@@ -269,7 +269,7 @@ def __init__(self, filename='POSCAR'):
269269
def load(self):
270270
"获取文件数据信息"
271271
"Load all information in POSCAR."
272-
with open(self.filename, 'r') as f:
272+
with open(self.filename(), 'r') as f:
273273
content_list = f.readlines()
274274
#get scale factor
275275
bases_const = float(content_list[1])
@@ -415,7 +415,7 @@ def __init__(self, filename='XDATCAR'):
415415
self.load()
416416

417417
def load(self):
418-
with open(self.filename, 'r') as f:
418+
with open(self.filename(), 'r') as f:
419419
# read lattice info
420420
self.system = f.readline().strip()
421421
self.bases_const = float(f.readline().strip())
@@ -432,7 +432,7 @@ def load(self):
432432

433433
def __iter__(self):
434434
"generator which yield step number and iterative data."
435-
with open(self.filename, 'r') as f:
435+
with open(self.filename(), 'r') as f:
436436
# pass info lines
437437
for i in xrange(self.info_nline):
438438
f.readline()

vaspy/electro.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Provide electro-related file class which do operations on these files.
55
========================================================================
66
Written by PytLab <shaozhengjiang@gmail.com>, September 2015
7-
Updated by PytLab <shaozhengjiang@gmail.com>, October 2015
7+
Updated by PytLab <shaozhengjiang@gmail.com>, May 2016
88
========================================================================
99
1010
"""
@@ -131,7 +131,7 @@ def tofile(self):
131131
content = ''
132132
for datalist in data:
133133
content += ('%12.8f'*ndata + '\n') % tuple(datalist)
134-
with open(self.filename, 'w') as f:
134+
with open(self.filename(), 'w') as f:
135135
f.write(content)
136136

137137
return
@@ -189,12 +189,12 @@ def __init__(self, filename='ELFCAR'):
189189
plot_field method, plot scalar field for elf data
190190
============== =============================================
191191
"""
192-
PosCar.__init__(self, filename=filename)
192+
super(self.__class__, self).__init__(filename)
193193

194194
def load(self):
195195
"Rewrite load method"
196196
PosCar.load(self)
197-
with open(self.filename, 'r') as f:
197+
with open(self.filename(), 'r') as f:
198198
for i in xrange(self.totline):
199199
f.readline()
200200
#get dimension of 3d array

vaspy/incar.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ def __init__(self, filename='INCAR'):
2727
filename string, name of INCAR file
2828
============ =======================================
2929
"""
30-
VasPy.__init__(self, filename)
30+
super(self.__class__, self).__init__(filename)
31+
self.__filename = filename
3132
self.load()
3233

3334
def load(self):
3435
"Load all data in INCAR."
3536
tot_pnames, tot_datas = [], []
36-
with open(self.filename, 'r') as f:
37+
with open(self.__filename, 'r') as f:
3738
for line in f:
3839
matched = self.rdata(line)
3940
if matched:
@@ -43,10 +44,22 @@ def load(self):
4344
# set attrs
4445
for pname, data in zip(tot_pnames, tot_datas):
4546
setattr(self, pname, data)
46-
self.pnames = tot_pnames
47+
self.__pnames = tot_pnames
4748

4849
return
4950

51+
def pnames(self):
52+
"""
53+
Query function for all parameter names.
54+
"""
55+
return self.__pnames
56+
57+
def file_name(self):
58+
"""
59+
Query function for all INCAR path names.
60+
"""
61+
return self.__filename
62+
5063
@staticmethod
5164
def rdata(line):
5265
"Get INCAR data(s) in a line."
@@ -75,7 +88,7 @@ def rdata(line):
7588

7689
def set(self, pname, data):
7790
"""
78-
Set a named property of InCar object.
91+
Set a named parameter of InCar object.
7992
8093
Example:
8194
--------
@@ -89,7 +102,7 @@ def set(self, pname, data):
89102

90103
def add(self, pname, data):
91104
"""
92-
Add a new property name to InCar object.
105+
Add a new parameter name to InCar object.
93106
94107
Example:
95108
--------
@@ -100,20 +113,20 @@ def add(self, pname, data):
100113
print ("Waring: %s is already in INCAR, " +
101114
"set to %s" % (pname, data))
102115
else:
103-
self.pnames.append(pname)
116+
self.__pnames.append(pname)
104117
setattr(self, pname, data)
105118

106119
return
107120

108121
def tofile(self):
109122
"Create INCAR file."
110123
content = '# Created by VASPy\n'
111-
for pname in self.pnames:
124+
for pname in self.__pnames:
112125
if not hasattr(self, pname):
113126
raise ValueError('Unknown parameter: %s' % pname)
114127
data = str(getattr(self, pname))
115128
content += '%s = %s\n' % (pname, data)
116-
with open('INCAR', 'w') as f:
129+
with open(self.__filename, 'w') as f:
117130
f.write(content)
118131

119132
return

vaspy/iter.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Provide iteration-related file class which do operations on these files.
55
========================================================================
66
Written by PytLab <shaozhengjiang@gmail.com>, August 2015
7-
Updated by PytLab <shaozhengjiang@gmail.com>, September 2015
7+
Updated by PytLab <shaozhengjiang@gmail.com>, May 2016
88
========================================================================
99
1010
"""
@@ -36,7 +36,7 @@ def __init__(self, filename='OSZICAR'):
3636
plot() method, 对数据绘图
3737
============ =======================================
3838
"""
39-
VasPy.__init__(self, filename)
39+
super(self.__class__, self).__init__(filename)
4040

4141
#set regex patterns
4242
float_regex = r'[\+|-]?\d*\.\d*(?:[e|E][\+|-]?\d+)?'
@@ -69,7 +69,7 @@ def match(self, line):
6969

7070
def load(self):
7171
"加载文件数据信息"
72-
with open(self.filename, 'r') as f:
72+
with open(self.filename(), 'r') as f:
7373
content = ''
7474
for line in f:
7575
eq_tuples = self.match(line)
@@ -156,7 +156,7 @@ def __init__(self, filename='OUTCAR'):
156156

157157
def load(self):
158158
#locate informations
159-
with open(self.filename, 'r') as f:
159+
with open(self.filename(), 'r') as f:
160160
total_forces = []
161161
tforce_regex = \
162162
re.compile(r'FORCES: max atom, RMS\s+(\d+\.\d+)\s+\d+\.\d+\s*')
@@ -180,7 +180,7 @@ def load(self):
180180
total_forces = np.array(total_forces)
181181
#atom forces
182182
atom_forces = []
183-
with open(self.filename, 'r') as f:
183+
with open(self.filename(), 'r') as f:
184184
for i, line in enumerate(f):
185185
if i > fbegin+1:
186186
if '-'*10 in line:

vaspy/matstudio.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Provide Material Studio markup file class which do operations on these files.
55
=============================================================================
66
Written by PytLab <shaozhengjiang@gmail.com>, August 2015
7-
Updated by PytLab <shaozhengjiang@gmail.com>, September 2015
7+
Updated by PytLab <shaozhengjiang@gmail.com>, May 2016
88
==============================================================
99
1010
"""
@@ -43,12 +43,12 @@ def __init__(self, filename):
4343
bases np.array, basis vectors of space, dtype=np.float64
4444
============ =======================================================
4545
"""
46-
AtomCo.__init__(self, filename)
46+
super(self.__class__, self).__init__(filename)
4747
self.load()
4848

4949
def load(self):
5050
# get element tree
51-
tree = ET.ElementTree(file=self.filename)
51+
tree = ET.ElementTree(file=self.filename())
5252
self.tree = tree
5353
# MS version info
5454
root = tree.getroot()

0 commit comments

Comments
 (0)