コード例 #1
0
ファイル: parseB.py プロジェクト: bzhurov/auto
 def readFilename(self,filename):
     inputfile = AUTOutil.openFilename(filename,"r")
     self.read(inputfile)
     inputfile.close()
コード例 #2
0
    def __init__(self, filename):
        if isinstance(filename, str):
            inputfile = AUTOutil.openFilename(filename, "rb")
        else:
            inputfile = filename
        self.inputfile = inputfile
        self.name = inputfile.name
        self.solutions = []

        # We now go through the file and read the solutions.
        prev = None
        # for fort.8 we need to read everything into memory; otherwise load the
        # data on demand from disk when we really need it
        # on Windows always load everything because deleting open files is
        # impossible there
        inmemory = (os.path.basename(inputfile.name) == 'fort.8'
                    or sys.platform in ['cygwin', 'win32'])
        while len(inputfile.read(1)) > 0:
            line = inputfile.readline()
            if not line: raise PrematureEndofData
            try:
                header = list(map(int, line.split()))
            except ValueError:
                raise PrematureEndofData
            if len(header) < 10:
                raise PrematureEndofData
            if len(header) == 10:
                # This is the case for AUTO94 and before
                header = header + [NPAR]
            numLinesPerEntry = header[8]
            start_of_data = inputfile.tell()
            if prev is not None and all(
                [header[i] == prevheader[i] for i in [4, 6, 7, 8, 11]]):
                # guess the end from the previous solution
                end += inputfile.tell() - prev
                # See if the guess for the solution end is correct
                inputfile.seek(end)
                data = inputfile.readline().split()
                # This is where we detect the end of the file
                if len(data) == 0:
                    data = inputfile.read(1)
                if len(data) != 0:
                    try:
                        # Check length of line...
                        if len(data) != 12 and len(data) != 16:
                            raise IncorrectHeaderLength
                        # and the fact they are all integers
                        map(int, data)
                        # and the fact that NCOL*NTST+1=NTPL
                        if int(data[9]) * int(data[10]) + 1 != int(data[6]):
                            end = None
                        # If it passes all these tests we say it is a header line
                        # and we can read quickly
                    except:
                        # otherwise the guessed end is not valid
                        end = None
            else:
                end = None
            data = None
            if end is None:
                # We skip the correct number of lines in the entry to
                # determine its end.
                inputfile.seek(start_of_data)
                if inmemory:
                    data = "".encode("ascii").join([
                        inputfile.readline() for i in range(numLinesPerEntry)
                    ])
                else:
                    for i in range(numLinesPerEntry):
                        inputfile.readline()
                end = inputfile.tell()
            elif inmemory:
                inputfile.seek(start_of_data)
                data = inputfile.read(end - start_of_data)
            else:
                inputfile.seek(end)
            if data is None:
                data = (start_of_data, end)
            self.solutions.append({'header': header, 'data': data})
            prev = start_of_data
            prevheader = header