Python vs Modula-2

(1) Reading and scaling a data file

To (1A)     To Workbench Python

<Task>

We have a text file tab2.5.csv (contents) in which experimental data (xi, yi, zi) are written from line 2 and beyond in the comma-separated format. The task is to read yi to form a vector (y0,y1,・・・,yndata-1) and to multiply each component by a.

The number of components, ndata, can be judged from the file, but is surely less than 1000. The number of items is at most three. The size information is important to Modula-2.

<What if Excel is used>

If you click this file, Excel will start automatically. You will find data in columns A, B, C from rows 2, 3, .... If you want to have the data in column D scaled, input "=B2*$D$1" to cell D2, where a is assumed to be stored in cell D1. Just drag D2 downward and you will have scaled values.

<Python (Object Oriented Programming)>

[Module] fileRead.py

class readtest:
   
    def __init__(self,fname): 
        w = []
        file = open(fname, 'r')
        nline = 0
        while (True):
            line = file.readline()
            print(line, end="")
            if len(line)==0:
                break
            nline += 1
            if nline == 1:                
                self.header = line
            else:
                result = line.split(',')
                w.append(float(result[1]))  
        file.close()
        nline -= 1
        self.ndata = nline
        self.w = w
[Main] mytest.py
def scaling(x, a):
        n = len(x)
        out = x.copy() 
        for i in range(n):
            out[i] = a*out[i]
        return out

f = fileRead.readtest('tab2.5.csv')
ndata = f.ndata
y = f.w
yy = scaling(y, 2.)
print(y,', ',yy)
<Modula-2 (Structured Programming)>

DEFINITION MODULE fileRead.DEF

VAR header: ARRAY[0..79] OF CHAR;

PROCEDURE readtest(fname: ARRAY OF CHAR;
                   VAR ndata: INTEGER; 
                   VAR y: ARRAY OF LONGREAL);

IMPLEMENTATION MODULE fileRead.MOD

PROCEDURE readtest(fname: ARRAY OF CHAR;
  VAR ndata: INTEGER; VAR y: ARRAY OF LONGREAL);
VAR
  f : FIO.File;
  line, s: ARRAY[0..79] OF CHAR;
  nline,i: INTEGER;
  OK : BOOLEAN;
BEGIN
  IF NOT FIO.Exists(fname) THEN RETURN END;
  f := FIO.Open(fname);
  nline := 0; ndata := 0;
  LOOP
    FIO.RdStr(f,line);
    WrStr(line); WrLn;
    IF Str.Length(line)=0 THEN EXIT END;
    INC(nline);
    IF nline=1 THEN Str.Copy(header,line);
    ELSE
      Str.Item(s, line, Str.CHARSET{','}, 1);
	  y[ndata]:= Str.StrToReal(s,OK);
	  INC(ndata);
    END;
  END;
  FIO.Close(f);
END readtest;

[Main] mytest.MOD

IMPORT fileRead;
IMPORT WrStr, WrLn, WrLngReal, WrInt;

PROCEDURE scaling(VAR yout: ARRAY OF LONGREAL;
			yin: ARRAY OF LONGREAL;
			n: INTEGER;
			a: LONGREAL);
  VAR
    i: INTEGER;
  BEGIN
    FOR i:=0 TO n-1 DO
      yout[i]:= a*yin[i];
    END;
  END scaling;      	
  
VAR
  ndata: INTEGER;
  y,yy : ARRAY[0..999] OF LONGREAL;
  
BEGIN
  fileRead.readtest('tab2.5.csv', ndata, y);
  scaling(yy, y, ndata, 3.0);
  WrStr('ndata='); WrInt(ndata,1); WrLn;
  FOR i:=0 TO ndata-1 DO
    WrLngReal(y[i],5,10); WrStr(', '); 
	WrLngReal(yy[i],5,10); WrLn;
  END;
<Remark>
  • Changing "out = x.copy()" to "out = x" is invalid.
  • The transfer of variables seems to be clearer.

9-19-2023, 11-9-2022, S. Hayashi