Skip to content

Commit

Permalink
Changed classes that were previously subclassed from 'file' to contai…
Browse files Browse the repository at this point in the history
…n a file object instead (as subclassing from file is no longer allowed in Python3)
  • Loading branch information
acroucher committed Nov 28, 2016
1 parent 84fc4fb commit 447c50c
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 130 deletions.
18 changes: 15 additions & 3 deletions fixed_format_file.py
Expand Up @@ -79,7 +79,7 @@ def read_function_dict(floatfn = default_read_float, intfn = default_read_int,
default_read_function = read_function_dict()
fortran_read_function = read_function_dict(fortran_read_float, fortran_read_int)

class fixed_format_file(file):
class fixed_format_file(object):

"""Class for fixed format text file. Values from the file may be parsed into variables,
according to a specification dictionary. The keys of the specification dictionary are
Expand All @@ -95,7 +95,19 @@ def __init__(self, filename, mode, specification, read_function = default_read_f
self.specification = specification
self.read_function = read_function
self.preprocess_specification()
super(fixed_format_file, self).__init__(filename, mode)
self.file = open(filename, mode)

def readline(self):
"""Returns next line from file."""
return self.file.readline()

def write(self, s):
"""Writes string s to file."""
self.file.write(s)

def close(self):
"""Closes file."""
self.file.close()

def preprocess_specification(self):
"""Pre-process specifications to speed up parsing."""
Expand Down Expand Up @@ -128,7 +140,7 @@ def write_values_to_string(self, vals, linetype):

def read_values(self, linetype):
"""Reads a line from the file, parses it and returns the values."""
line = self.readline()
line = self.file.readline()
return self.parse_string(line,linetype)

def write_values(self, vals, linetype):
Expand Down
16 changes: 10 additions & 6 deletions t2data.py
Expand Up @@ -104,21 +104,25 @@ def __init__(self, filename, mode, read_function = default_read_function):
super(t2_extra_precision_data_parser,self).__init__(filename, mode, t2data_extra_precision_format_specification, read_function)

import struct
class fortran_unformatted_file(file):
class fortran_unformatted_file(object):
"""Class for 'unformatted' binary file written by Fortran. These are different from plain binary files
in that the byte length of each 'record' is written at its start and end."""
def __init__(self, filename, mode):
self.file = open(filename, mode)
def close(self):
self.file.close()
def readrec(self,fmt):
nb,=struct.unpack('i',self.read(4))
packed=self.read(nb)
self.read(4)
nb,=struct.unpack('i',self.file.read(4))
packed=self.file.read(nb)
self.file.read(4)
return struct.unpack(fmt,packed)
def writerec(self,fmt,val):
nb=struct.calcsize(fmt)
if isinstance(val,(tuple,list,np.ndarray)):
packed=struct.pack(fmt,*val)
else: packed=struct.pack(fmt,val)
head=struct.pack('i',nb)
self.write(''.join((head,packed,head)))
self.file.write(''.join((head,packed,head)))

class t2generator(object):
"""TOUGH2 generator (source or sink)"""
Expand Down Expand Up @@ -436,7 +440,7 @@ def read_parameters(self,infile):
"""Reads simulation parameters"""
spec=['param1','param1_autough2'][self.type=='AUTOUGH2']
infile.read_value_line(self.parameter,spec)
mops=ljust(self.parameter['_option_str'].rstrip(),24).replace(' ','0')
mops = self.parameter['_option_str'].rstrip().ljust(24).replace(' ','0')
self.parameter['option']=np.array([0]+[int(mop) for mop in mops],int8)
infile.read_value_line(self.parameter,'param2')
if (self.parameter['print_block'] is not None) and (self.parameter['print_block'].strip()==''):
Expand Down

0 comments on commit 447c50c

Please sign in to comment.