本文整理汇总了Python中version.Version.write方法的典型用法代码示例。如果您正苦于以下问题:Python Version.write方法的具体用法?Python Version.write怎么用?Python Version.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类version.Version
的用法示例。
在下文中一共展示了Version.write方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ParamFile
# 需要导入模块: from version import Version [as 别名]
# 或者: from version.Version import write [as 别名]
class ParamFile(object):
"""
Hold the data in a PSCF parameter file.
The constructor reads a PSCF parameter file and stores the values of
parameters as attributes with names that are the same as the variable
names in the parameter file. Parameters that are stored in PSCF as
1D arrays are stored in list parameters of the same name. Parameters
that are stored in PSCF as matrices are stored as lists of lists.
Note that list indices are numbered from 0 (C/python convention),
rather than from 1 as in PSCF (Fortran convention), so all indices
are off by one relative to the values used in the PSCF source code.
Construction: To construct an object from a param file named 'param':
> param = ParamFile('param')
Attributes: After construction, param.kuhn[2] is the statistical
segment length of the third monomer type, and param.block_length[0][1]
is the length of the 2nd block of the first chain species.
An instance of the ParamFile class can be used to edit values of
parameters by reading one parameter file, modifying one or more
parameters, and then write the object to another file.
"""
def __init__(self,filename):
"""
Read and parse a PSCF parameter file, create an object to hold data.
The file with specified filename is opened and closed within body
of the constructor method
Argument:
filename - name of PSCF parameter file (string)
"""
self.file = open(filename, 'r')
self._io = IO()
file = self.file
self.N_chain = 0
self.N_solvent = 0
# Dictionary flags indicates which sections are present
# Keys are flag string values ('MONOMERS' etc.), values are all 1
self.flags = {}
# List sections lists section names (flags) in order read
self.sections = []
# Read version line
self.version = Version(self.file)
# Read input script section
next = 1
while next:
next = self.read_param_section(file)
self.file.close()
self.file = None
# Make sorted list of attribute names
self.att_names = self.__dict__.keys()
self.att_names.sort()
def write(self, file, major=1, minor=0):
'''
Write a parameter file, in the format used by PSCF.
If the "file" argument is a file object, it must be opened
for writing. If it is a file name string, a file of that
name will be opened and written to. In either case, the file
is closed upon return.
Argument:
file - output file object or file name.
'''
# If file argument is a string, open a file of that name
if type(file) == type('thing'):
temp = open(file,'w')
file = temp
self.file = file
self.version.major = major
self.version.minor = minor
self.version.write(file)
if self.flags.has_key('MONOMERS'):
file.write("\n%-20s\n" % 'MONOMERS')
self.output_monomers()
if self.flags.has_key('CHAINS'):
file.write("\n%-20s\n" % 'CHAINS')
self.output_chains()
if self.flags.has_key('SOLVENTS'):
file.write("\n%-20s\n" % 'SOLVENTS')
self.output_solvents()
if self.flags.has_key('COMPOSITION'):
file.write("\n%-20s\n" % 'COMPOSITION')
self.output_composition()
if self.flags.has_key('INTERACTION'):
file.write("\n%-20s\n" % 'INTERACTION')
self.output_interaction()
#.........这里部分代码省略.........
示例2: OutFile
# 需要导入模块: from version import Version [as 别名]
# 或者: from version.Version import write [as 别名]
class OutFile(ParamFile):
"""
Hold the data in a PSCF output summary file.
The constructor reads an output file and holds the data in
attributes with names that are the same as the names of the
variables in the output file.
Class Outfile is derived from ParamFile because the output
file format begins with a parameter file section, to which
a few output sections are added.
"""
def __init__(self,filename):
"""
Read and parse an output file, and create a corresponding object.
A file named file is opened, read, and closed within the method.
Argument:
filename -- name of PSCF output summary file (string)
"""
self.file = open(filename, 'r')
self._io = IO()
file = self.file
self.N_chain = 0
self.N_solvent = 0
# Dictionary flags indicates which sections are present
# Keys are flag string values ('MONOMERS' etc.), values are all 1
self.flags = {}
# List sections lists section names (flags) in order read
self.sections = []
# Read version line
self.version = Version(self.file)
# Read input parameter file sections
next = 1
while next:
next = self.read_param_section(file)
# Read additional output sections
next = 1
while next :
next = self.read_output_section()
self.file.close()
self.file = None
# Make sorted list of attribute names
self.att_names = self.__dict__.keys()
self.att_names.sort()
def write(self, file, major=1, minor=0):
"""
Write an output file.
Argument:
file -- file object or name of file.
If file is a file object, it must be opened for writing.
If file is a string, a file of that name is opened. In
either case, the file is closed upon return.
"""
# If file argument is a string, open a file of that name
if type(file) == type('thing'):
temp = open(file,'w')
file = temp
self.file = file
self.version.major = major
self.version.minor = minor
# Parameter file sections
self.version.write(file)
if self.flags.has_key('MONOMERS'):
file.write("\n%-15s\n" % 'MONOMERS')
self.output_monomers()
if self.flags.has_key('CHAINS'):
file.write("\n%-15s\n" % 'CHAINS')
self.output_chains()
if self.flags.has_key('SOLVENTS'):
file.write("\n%-15s\n" % 'SOLVENTS')
self.output_solvents()
if self.flags.has_key('COMPOSITION'):
file.write("\n%-15s\n" % 'COMPOSITION')
self.output_composition()
if self.flags.has_key('INTERACTION'):
file.write("\n%-15s\n" % 'INTERACTION')
self.output_interaction()
if self.flags.has_key('UNIT_CELL'):
file.write("\n%-15s\n" % 'UNIT_CELL')
self.output_unit_cell()
if self.flags.has_key('DISCRETIZATION'):
file.write("\n%-15s\n" % 'DISCRETIZATION')
self._output_vec( 'int', 'ngrid', self.dim)
self._output_var( 'real', 'chain_step')
#self._output_var( 'int', 'extrapolation_order')
if self.flags.has_key('BASIS'):
#.........这里部分代码省略.........
示例3: FieldFile
# 需要导入模块: from version import Version [as 别名]
# 或者: from version.Version import write [as 别名]
class FieldFile(object):
'''
Hold data in a PSCF field file.
A FieldFile object contains the data in a field file in the PSCF
symmetry-adapted Fourier expansion format (see web user manual).
It can be used to represent either omega (chemical potential) or
rho (volume fraction) fields. a
The constructor reads a field file, creates a FieldFile object to store
the data, and stores all of the parameters and field coefficients in
attributes of the object.
Attributes:
dim -- [int] number of periodic spatial directions
crystal_system -- [string] label of Bravais crystal system
N_cell_param -- [int] number of unit cell parameters
cell_param -- [list] list of real unit cell parameters
group -- [string] name of space group
N_monomer -- [int] number of monomer types
N_star -- [int] number of basis functions (or stars)
fields -- [list] list of list of coefficients
The attribute field[i] is is a list (or array) of coefficients
for a field associated with monomer type number i. The element
field[i][j] is the coefficient of basis function j in the field
associated with monomer type i.
'''
# "Public" methods
def __init__(self,filename):
'''
Read a PSCF symmetry-adapted field file, and create a new object.
Argument:
filename -- name of file
The file named filename is opened and closed within this function.
'''
self.file = open(filename, 'r')
self._io = IO()
file = self.file
# Read version line
self.version = Version(self.file)
self._input_unit_cell()
self.group_name = self._input_var('char')
self.N_monomer = self._input_var('int')
self.N_star = self._input_var('int')
# Define empty lists
self.fields = []
self.waves = []
self.counts = []
for i in range(self.N_star):
data = file.readline().split()
if len(data) != self.N_monomer + self.dim + 1:
raise IoException('Incorrect number of elements in field line')
j = 0
# Read field coefficients
self.fields.append([])
for k in range(self.N_monomer):
value = float(data[j])
self.fields[i].append(value)
j += 1
# Read field coefficients
self.waves.append([])
for k in range(self.dim):
value = int(data[j])
self.waves[i].append(value)
j += 1
# Read star_count
self.counts.append(int(data[j]))
self.file.close()
self.file = None
def write(self, file, major=1, minor=0):
'''
PURPOSE
Write field to file in PSCF symmetry-adapted format.
ARGUMENTS
file - file object or file name string
major - major file format version number
minor - minor file format version number
COMMENT
if file is a field object, it must be open for writing
'''
# If file argument is a string, open a file of that name
if type(file) == type('thing'):
temp = open(file,'w')
file = temp
#.........这里部分代码省略.........