Changeset 23689


Ignore:
Timestamp:
02/04/19 12:50:23 (6 years ago)
Author:
bdef
Message:

CHG: py3 migration

Location:
issm/trunk-jpl/src/py3
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/py3/archive/arch.py

    r23677 r23689  
    11import numpy as np
    2 import math
    32import struct
    4 import sys
    5 import os
     3from os import path
    64from collections import OrderedDict
    75
     
    1816        # open file
    1917        try:
    20                 if not os.path.isfile(filename):
     18                if not path.isfile(filename):
    2119                        fid=open(filename,'wb')
    2220                else:
     
    3129                name=args[2*i]
    3230                write_field_name(fid,name)
    33                
     31
    3432                # write data associated with field name
    3533                data=args[2*i+1]
     
    4341                else:
    4442                        raise ValueError("archwrite : error writing data, invalid code entered '%d'" % code)
    45        
     43
    4644        fid.close()
    4745
     
    5553        """
    5654        try:
    57                 if os.path.isfile(filename):
     55                if path.isfile(filename):
    5856                        fid=open(filename,'rb')
    5957                else:
     
    6159        except IOError as e:
    6260                raise IOError("archread error : could not open file '%s' to read from" % filename)
    63        
     61
    6462        archive_results=[]
    6563
    6664        # read first result
    6765        result=read_field(fid)
     66
    6867        while result:
    6968                if fieldname == result['field_name']:
     
    7170                        archive_results=result['data']; # we only want the data
    7271                        break
    73                
     72
    7473                # read next result
    7574                result=read_field(fid)
    76        
     75
    7776        # close file
    7877        fid.close()
    79        
     78
    8079        return archive_results
    8180# }}}
     
    8887        """
    8988        try:
    90                 if os.path.isfile(filename):
     89                if path.isfile(filename):
    9190                        fid=open(filename,'rb')
    9291                else:
     
    9493        except IOError as e:
    9594                raise IOError("archread error : could not open file '%s' to read from" % filename)
    96        
     95
    9796        print('Source file: ')
    9897        print('\t{0}'.format(filename))
     
    106105                # go to next result
    107106                result=read_field(fid)
    108        
     107
    109108        # close file
    110109        fid.close()
     
    112111# }}}
    113112
    114 # Helper functions 
     113# Helper functions
    115114def write_field_name(fid,data): # {{{
    116115        """
     
    121120        reclen=len(data)+4+4
    122121        fid.write(struct.pack('>i',reclen))
    123        
     122
    124123        # write format code
    125124        code=format_archive_code(data);
     
    130129        # write string length, and then the string
    131130        fid.write(struct.pack('>i',len(data)))
    132         fid.write(struct.pack('>%ds' % len(data),data))
     131        fid.write(struct.pack('>{}s'.format(len(data)),data.encode('utf8')))
    133132# }}}
    134133def write_scalar(fid,data): # {{{
     
    143142        # write the format code (2 for scalar)
    144143        fid.write(struct.pack('>i',2))
    145        
     144
    146145        # write the double
    147146        fid.write(struct.pack('>d',data))
     
    158157        elif isinstance(data,(list,tuple)):
    159158                data=np.array(data).reshape(-1,)
    160        
     159
    161160        if np.ndim(data) == 1:
    162161                if np.size(data):
     
    164163                else:
    165164                        data=data.reshape(0,0)
    166        
     165
    167166        # get size of data
    168167        sz=data.shape
     
    175174                raise ValueError("archwrite error : can not write vector to binary file because it is too large")
    176175        fid.write(struct.pack('>i',reclen))
    177        
     176
    178177        # write format code
    179178        fid.write(struct.pack('>i',3))
     
    204203                        raise ValueError('archread error : a string was not present at the start of the arch file')
    205204                namelen=struct.unpack('>i',fid.read(struct.calcsize('>i')))[0]
    206                 fieldname=struct.unpack('>%ds' % namelen,fid.read(namelen))[0]
    207                
     205                fieldname=struct.unpack('>{}s'.format(namelen),fid.read(namelen))[0]
     206
    208207                # then, read the data
    209208                datalen=struct.unpack('>i',fid.read(struct.calcsize('>i')))[0]
     
    211210
    212211                if data_type==2:
    213                         # unpack scalar
     212                        # struct.upack scalar
    214213                        data=struct.unpack('>d',fid.read(struct.calcsize('>d')))[0]
    215214                elif data_type==3:
     
    218217                        raw_data=np.zeros(shape=(rows,cols),dtype=float)
    219218                        for i in range(rows):
    220                                 raw_data[i,:]=struct.unpack('>%dd' % cols,fid.read(cols*struct.calcsize('>d')))
    221                         # The matrix will be unpacked in order and will be filled left -> right by column
     219                                raw_data[i,:]=struct.unpack('>{}d'.format(cols),fid.read(cols*struct.calcsize('>d')))
     220                        # The matrix will be struct.upacked in order and will be filled left -> right by column
    222221                        # We need to reshape and transpose the matrix so it can be read correctly
    223222                        data=raw_data.reshape(raw_data.shape[::-1]).T
    224223                else:
    225224                        raise TypeError("Cannot read data type %d" % data_type)
    226                        
     225
    227226                # give additional data to user
    228227                if data_type==2:
     
    234233
    235234                result=OrderedDict()
    236                 result['field_name']=fieldname
     235                result['field_name']=fieldname.decode('utf8')
    237236                result['size']=data_size
    238237                result['data_type']=data_type_str
  • issm/trunk-jpl/src/py3/classes/pairoptions.py

    r23670 r23689  
    55        """
    66        PAIROPTIONS class definition
    7  
     7
    88           Usage:
    99              pairoptions=pairoptions();
     
    2727        # }}}
    2828        def __repr__(self):    # {{{
    29                 s="   functionname: '%s'\n" % self.functionname
     29                s="   functionname: '{}'\n".format(self.functionname)
    3030                if self.list:
    31                         s+="   list: (%ix%i)\n\n" % (len(self.list),2)
     31                        s+="   list: ({}x{}) \n\n".format(len(self.list),2)
    3232                        for item in self.list.items():
    33                                 if   isinstance(item[1],str):
    34                                         s+="     field: %-10s value: '%s'\n" % (item[0],item[1])
    35                                 elif isinstance(item[1],(bool,int,float)):
    36                                         s+="     field: %-10s value: %g\n" % (item[0],item[1])
    37                                 else:
    38                                         s+="     field: %-10s value: %s\n" % (item[0],type(item[1]))
     33                                #if   isinstance(item[1],str):
     34                                s+="     field: {} value: '{}'\n".format((item[0],item[1]))
     35                                # elif isinstance(item[1],(bool,int,float)):
     36                                #       s+="     field: %-10s value: %g\n" % (item[0],item[1])
     37                                # else:
     38                                #       s+="     field: %-10s value: %s\n" % (item[0],type(item[1]))
    3939                else:
    4040                        s+="   list: empty\n"
     
    4646                #check length of input
    4747                if len(arg) % 2:
    48                         raise TypeError('Invalid parameter/value pair arguments') 
    49                 numoptions = len(arg)/2
     48                        raise TypeError('Invalid parameter/value pair arguments')
     49                numoptions = int(len(arg)/2)
    5050
    5151                #go through arg and build list of objects
     
    5555                        else:
    5656                                #option is not a string, ignore it
    57                                 print("WARNING: option number %d is not a string and will be ignored." % (i+1))
     57                                print("WARNING: option number {} is not a string and will be ignored.".format(i+1))
    5858        # }}}
    5959        def addfield(self,field,value):    # {{{
     
    6161                if isinstance(field,str):
    6262                        if field in self.list:
    63                                 print("WARNING: field '%s' with value=%s exists and will be overwritten with value=%s." % (field,str(self.list[field]),str(value)))
     63                                print("WARNING: field '{}' with value={} exists and will be overwritten with value={}.".format(field,str(self.list[field]),str(value)))
    6464                        self.list[field] = value
    6565        # }}}
     
    8787                """EXIST - check if the option exist"""
    8888
    89                 #some argument checking: 
     89                #some argument checking:
    9090                if field == None or field == '':
    9191                        raise ValueError('exist error message: bad usage');
     
    102102                """
    103103                GETOPTION - get the value of an option
    104        
     104
    105105                Usage:
    106106                   value=options.getfieldvalue(field,default)
    107          
     107
    108108                Find an option value from a field. A default option
    109109                can be given in input if the field does not exist
    110          
     110
    111111                Examples:
    112112                   value=options.getfieldvalue(options,'caxis')
     
    114114                """
    115115
    116                 #some argument checking: 
     116                #some argument checking:
    117117                if field == None or field == '':
    118118                        raise ValueError('getfieldvalue error message: bad usage');
     
    134134                """
    135135                REMOVEFIELD - delete a field in an option list
    136          
     136
    137137                Usage:
    138138                   obj=removefield(self,field,warn)
    139          
     139
    140140                if warn==1 display an info message to warn user that
    141141                some of his options have been removed.
  • issm/trunk-jpl/src/py3/consistency/checkfield.py

    r23670 r23689  
    22import os
    33from pairoptions import pairoptions
     4from operator import attrgetter
    45import MatlabFuncs as m
    56
     
    4142        else:
    4243                fieldname=options.getfieldvalue('fieldname')
    43                 exec("field=md.{}".format(fieldname))
     44
     45                field=attrgetter(fieldname)(md)
     46#               exec("field=md.{}".format(fieldname),namespace)
    4447
    4548        if isinstance(field,(bool,int,float)):
  • issm/trunk-jpl/src/py3/solve/WriteData.py

    r23670 r23689  
    11import numpy as np
    2 import struct
    3 import pairoptions
    4 import MatlabFuncs as m
     2from struct import pack,unpack
     3from pairoptions import pairoptions
    54
    65def WriteData(fid,prefix,*args):
     
    1312
    1413        #process options
    15         options=pairoptions.pairoptions(*args)
     14        options=pairoptions(*args)
    1615
    1716        #Get data properties
     
    3130                name = options.getfieldvalue('name')
    3231
    33         format  = options.getfieldvalue('format')
     32        datatype  = options.getfieldvalue('format')
    3433        mattype = options.getfieldvalue('mattype',0)    #only required for matrices
    3534        timeserieslength = options.getfieldvalue('timeserieslength',-1)
     
    5655
    5756        #Step 1: write the enum to identify this record uniquely
    58         fid.write(struct.pack('i',len(name)))
    59         fid.write(struct.pack('%ds' % len(name),name))
     57        fid.write(pack('>i',len(name)))
     58        fid.write(pack('>{}s'.format(len(name)),name.encode()))
     59        # print(name)
     60        # print(pack('>{}s'.format(len(name)),name)
    6061
    6162        #Step 2: write the data itself.
    62         if   m.strcmpi(format,'Boolean'):    # {{{
     63        if datatype=='Boolean':    # {{{
    6364#               if len(data) !=1:
    6465#                       raise ValueError('field %s cannot be marshalled as it has more than one element!' % name[0])
    6566
    6667                #first write length of record
    67                 fid.write(struct.pack('i',4+4))  #1 bool (disguised as an int)+code
    68 
    69                 #write data code:
    70                 fid.write(struct.pack('i',FormatToCode(format)))
     68                fid.write(pack('>i',4+4))  #1 bool (disguised as an int)+code
     69
     70                #write data code:
     71                fid.write(pack('>i',FormatToCode(datatype)))
    7172
    7273                #now write integer
    73                 fid.write(struct.pack('i',int(data)))  #send an int, not easy to send a bool
    74                 # }}}
    75 
    76         elif m.strcmpi(format,'Integer'):    # {{{
     74                fid.write(pack('>i',int(data)))  #send an int, not easy to send a bool
     75                # }}}
     76
     77        elif datatype=='Integer':    # {{{
    7778#               if len(data) !=1:
    7879#                       raise ValueError('field %s cannot be marshalled as it has more than one element!' % name[0])
    7980
    8081                #first write length of record
    81                 fid.write(struct.pack('i',4+4))  #1 integer + code
    82 
    83                 #write data code:
    84                 fid.write(struct.pack('i',FormatToCode(format)))
     82                fid.write(pack('>i',4+4))  #1 integer + code
     83
     84                #write data code:
     85                fid.write(pack('>i',FormatToCode(datatype)))
    8586
    8687                #now write integer
    87                 fid.write(struct.pack('i',data))
    88                 # }}}
    89 
    90         elif m.strcmpi(format,'Double'):    # {{{
     88                fid.write(pack('>i',int(data))) #force an int,
     89                # }}}
     90
     91        elif datatype=='Double':    # {{{
    9192#               if len(data) !=1:
    9293#                       raise ValueError('field %s cannot be marshalled as it has more than one element!' % name[0])
    9394
    9495                #first write length of record
    95                 fid.write(struct.pack('i',8+4))  #1 double+code
    96 
    97                 #write data code:
    98                 fid.write(struct.pack('i',FormatToCode(format)))
     96                fid.write(pack('>i',8+4))  #1 double+code
     97
     98                #write data code:
     99                fid.write(pack('>i',FormatToCode(datatype)))
    99100
    100101                #now write double
    101                 fid.write(struct.pack('d',data))
    102                 # }}}
    103 
    104         elif m.strcmpi(format,'String'):    # {{{
    105                 #first write length of record
    106                 fid.write(struct.pack('i',len(data)+4+4))  #string + string size + code
    107 
    108                 #write data code:
    109                 fid.write(struct.pack('i',FormatToCode(format)))
     102                fid.write(pack('>d',data))
     103                # }}}
     104
     105        elif datatype=='String':    # {{{
     106                #first write length of record
     107                fid.write(pack('>i',len(data)+4+4))  #string + string size + code
     108
     109                #write data code:
     110                fid.write(pack('>i',FormatToCode(datatype)))
    110111
    111112                #now write string
    112                 fid.write(struct.pack('i',len(data)))
    113                 fid.write(struct.pack('%ds' % len(data),data))
    114                 # }}}
    115 
    116         elif m.strcmpi(format,'BooleanMat'):    # {{{
    117 
    118                 if   isinstance(data,bool):
     113                fid.write(pack('>i',len(data)))
     114                fid.write(pack('>{}s'.format(len(data)),data.encode()))
     115                # }}}
     116
     117        elif datatype in ['IntMat','BooleanMat']:    # {{{
     118
     119                if   isinstance(data,(int,bool)):
    119120                        data=np.array([data])
    120121                elif isinstance(data,(list,tuple)):
     
    133134
    134135                #first write length of record
    135                 fid.write(struct.pack('i',4+4+8*np.product(s)+4+4))    #2 integers (32 bits) + the double matrix + code + matrix type
     136                fid.write(pack('>i',4+4+8*np.product(s)+4+4))    #2 integers (32 bits) + the double matrix + code + matrix type
    136137
    137138                #write data code and matrix type:
    138                 fid.write(struct.pack('i',FormatToCode(format)))
    139                 fid.write(struct.pack('i',mattype))
     139                fid.write(pack('>i',FormatToCode(datatype)))
     140                fid.write(pack('>i',mattype))
    140141
    141142                #now write matrix
    142                 if np.ndim(data)==1:
    143                         fid.write(struct.pack('i',s[0]))
    144                         fid.write(struct.pack('i',1))
     143                if np.ndim(data) == 1:
     144                        fid.write(pack('>i',s[0]))
     145                        fid.write(pack('>i',1))
    145146                        for i in range(s[0]):
    146                                 fid.write(struct.pack('d',float(data[i])))    #get to the "c" convention, hence the transpose
    147                 else:
    148                         fid.write(struct.pack('i',s[0]))
    149                         fid.write(struct.pack('i',s[1]))
     147                                fid.write(pack('>d',float(data[i])))    #get to the "c" convention, hence the transpose
     148                else:
     149                        fid.write(pack('>i',s[0]))
     150                        fid.write(pack('>i',s[1]))
    150151                        for i in range(s[0]):
    151152                                for j in range(s[1]):
    152                                         fid.write(struct.pack('d',float(data[i][j])))    #get to the "c" convention, hence the transpose
    153                 # }}}
    154 
    155         elif m.strcmpi(format,'IntMat'):    # {{{
    156 
    157                 if   isinstance(data,int):
    158                         data=np.array([data])
    159                 elif isinstance(data,(list,tuple)):
    160                         data=np.array(data).reshape(-1,)
    161                 if np.ndim(data) == 1:
    162                         if np.size(data):
    163                                 data=data.reshape(np.size(data),)
    164                         else:
    165                                 data=data.reshape(0,0)
    166 
    167                 #Get size
    168                 s=data.shape
    169                 #if matrix = NaN, then do not write anything
    170                 if np.ndim(data)==2 and np.product(s)==1 and np.all(np.isnan(data)):
    171                         s=(0,0)
    172 
    173                 #first write length of record
    174                 fid.write(struct.pack('i',4+4+8*np.product(s)+4+4))    #2 integers (32 bits) + the double matrix + code + matrix type
    175 
    176                 #write data code and matrix type:
    177                 fid.write(struct.pack('i',FormatToCode(format)))
    178                 fid.write(struct.pack('i',mattype))
    179 
    180                 #now write matrix
    181                 if np.ndim(data) == 1:
    182                         fid.write(struct.pack('i',s[0]))
    183                         fid.write(struct.pack('i',1))
    184                         for i in range(s[0]):
    185                                 fid.write(struct.pack('d',float(data[i])))    #get to the "c" convention, hence the transpose
    186                 else:
    187                         fid.write(struct.pack('i',s[0]))
    188                         fid.write(struct.pack('i',s[1]))
    189                         for i in range(s[0]):
    190                                 for j in range(s[1]):
    191                                         fid.write(struct.pack('d',float(data[i][j])))    #get to the "c" convention, hence the transpose
    192                 # }}}
    193 
    194         elif m.strcmpi(format,'DoubleMat'):    # {{{
     153                                        fid.write(pack('>d',float(data[i][j])))    #get to the "c" convention, hence the transpose
     154                # }}}
     155
     156        elif datatype=='DoubleMat':    # {{{
    195157
    196158                if   isinstance(data,(bool,int,float)):
     
    215177                        raise ValueError('field %s cannot be marshalled because it is larger than 4^31 bytes!' % enum)
    216178
    217                 fid.write(struct.pack('i',recordlength))  #2 integers (32 bits) + the double matrix + code + matrix type
     179                fid.write(pack('>i',recordlength))  #2 integers (32 bits) + the double matrix + code + matrix type
    218180
    219181                #write data code and matrix type:
    220                 fid.write(struct.pack('i',FormatToCode(format)))
    221                 fid.write(struct.pack('i',mattype))
     182                fid.write(pack('>i',FormatToCode(datatype)))
     183                fid.write(pack('>i',mattype))
    222184
    223185                #now write matrix
    224186                if np.ndim(data) == 1:
    225                         fid.write(struct.pack('i',s[0]))
    226                         fid.write(struct.pack('i',1))
     187                        fid.write(pack('>i',s[0]))
     188                        fid.write(pack('>i',1))
    227189                        for i in range(s[0]):
    228                                 fid.write(struct.pack('d',float(data[i])))    #get to the "c" convention, hence the transpose
    229                 else:
    230                         fid.write(struct.pack('i',s[0]))
    231                         fid.write(struct.pack('i',s[1]))
     190                                fid.write(pack('>d',float(data[i])))    #get to the "c" convention, hence the transpose
     191                else:
     192                        fid.write(pack('>i',s[0]))
     193                        fid.write(pack('>i',s[1]))
    232194                        for i in range(s[0]):
    233195                                for j in range(s[1]):
    234                                         fid.write(struct.pack('d',float(data[i][j])))    #get to the "c" convention, hence the transpose
    235                 # }}}
    236 
    237         elif m.strcmpi(format,'CompressedMat'):    # {{{
     196                                        fid.write(pack('>d',float(data[i][j])))    #get to the "c" convention, hence the transpose
     197                # }}}
     198
     199        elif datatype=='CompressedMat':    # {{{
    238200
    239201                if   isinstance(data,(bool,int,float)):
     
    264226                        raise ValueError('field %s cannot be marshalled because it is larger than 4^31 bytes!' % enum)
    265227
    266                 fid.write(struct.pack('i',recordlength))  #2 integers (32 bits) + the matrix + code + matrix type
     228                fid.write(pack('>i',recordlength))  #2 integers (32 bits) + the matrix + code + matrix type
    267229
    268230                #write data code and matrix type:
    269                 fid.write(struct.pack('i',FormatToCode(format)))
    270                 fid.write(struct.pack('i',mattype))
     231                fid.write(pack('>i',FormatToCode(datatype)))
     232                fid.write(pack('>i',mattype))
    271233
    272234                #Write offset and range
     
    282244                #now write matrix
    283245                if np.ndim(data) == 1:
    284                         fid.write(struct.pack('i',s[0]))
    285                         fid.write(struct.pack('i',1))
    286                         fid.write(struct.pack('d',float(offsetA)))
    287                         fid.write(struct.pack('d',float(rangeA)))
     246                        fid.write(pack('>i',s[0]))
     247                        fid.write(pack('>i',1))
     248                        fid.write(pack('>d',float(offsetA)))
     249                        fid.write(pack('>d',float(rangeA)))
    288250                        for i in range(s[0]-1):
    289                                 fid.write(struct.pack('B',int(A[i])))
    290 
    291                         fid.write(struct.pack('d',float(data[s[0]-1])))    #get to the "c" convention, hence the transpose
     251                                fid.write(pack('>B',int(A[i])))
     252
     253                        fid.write(pack('>d',float(data[s[0]-1])))    #get to the "c" convention, hence the transpose
    292254
    293255                elif np.product(s) > 0:
    294                         fid.write(struct.pack('i',s[0]))
    295                         fid.write(struct.pack('i',s[1]))
    296                         fid.write(struct.pack('d',float(offsetA)))
    297                         fid.write(struct.pack('d',float(rangeA)))
     256                        fid.write(pack('>i',s[0]))
     257                        fid.write(pack('>i',s[1]))
     258                        fid.write(pack('>d',float(offsetA)))
     259                        fid.write(pack('>d',float(rangeA)))
    298260                        for i in range(s[0]-1):
    299261                                for j in range(s[1]):
    300                                         fid.write(struct.pack('B',int(A[i][j])))    #get to the "c" convention, hence the transpose
     262                                        fid.write(pack('>B',int(A[i][j])))    #get to the "c" convention, hence the transpose
    301263
    302264                        for j in range(s[1]):
    303                                 fid.write(struct.pack('d',float(data[s[0]-1][j])))
    304 
    305                 # }}}
    306 
    307         elif m.strcmpi(format,'MatArray'):    # {{{
     265                                fid.write(pack('>d',float(data[s[0]-1][j])))
     266
     267                # }}}
     268
     269        elif datatype=='MatArray':    # {{{
    308270
    309271                #first get length of record
     
    324286
    325287                #write length of record
    326                 fid.write(struct.pack('i',recordlength))
    327 
    328                 #write data code:
    329                 fid.write(struct.pack('i',FormatToCode(format)))
     288                fid.write(pack('>i',recordlength))
     289
     290                #write data code:
     291                fid.write(pack('>i',FormatToCode(datatype)))
    330292
    331293                #write data, first number of records
    332                 fid.write(struct.pack('i',len(data)))
     294                fid.write(pack('>i',len(data)))
    333295
    334296                for matrix in data:
     
    343305
    344306                        if np.ndim(matrix) == 1:
    345                                 fid.write(struct.pack('i',s[0]))
    346                                 fid.write(struct.pack('i',1))
     307                                fid.write(pack('>i',s[0]))
     308                                fid.write(pack('>i',1))
    347309                                for i in range(s[0]):
    348                                         fid.write(struct.pack('d',float(matrix[i])))    #get to the "c" convention, hence the transpose
     310                                        fid.write(pack('>d',float(matrix[i])))    #get to the "c" convention, hence the transpose
    349311                        else:
    350                                 fid.write(struct.pack('i',s[0]))
    351                                 fid.write(struct.pack('i',s[1]))
     312                                fid.write(pack('>i',s[0]))
     313                                fid.write(pack('>i',s[1]))
    352314                                for i in range(s[0]):
    353315                                        for j in range(s[1]):
    354                                                 fid.write(struct.pack('d',float(matrix[i][j])))
    355                 # }}}
    356 
    357         elif m.strcmpi(format,'StringArray'):    # {{{
     316                                                fid.write(pack('>d',float(matrix[i][j])))
     317                # }}}
     318
     319        elif datatype=='StringArray':    # {{{
    358320
    359321                #first get length of record
     
    363325
    364326                #write length of record
    365                 fid.write(struct.pack('i',recordlength))
    366 
    367                 #write data code:
    368                 fid.write(struct.pack('i',FormatToCode(format)))
     327                fid.write(pack('>i',recordlength))
     328
     329                #write data code:
     330                fid.write(pack('>i',FormatToCode(datatype)))
    369331
    370332                #now write length of string array
    371                 fid.write(struct.pack('i',len(data)))
     333                fid.write(pack('>i',len(data)))
    372334
    373335                #now write the strings
    374336                for string in data:
    375                         fid.write(struct.pack('i',len(string)))
    376                         fid.write(struct.pack('%ds' % len(string),string))
     337                        fid.write(pack('>i',len(string)))
     338                        fid.write(pack('>{}s'.format(len(string)),string.encode()))
    377339                # }}}
    378340
    379341        else:    # {{{
    380                 raise TypeError('WriteData error message: data type: %d not supported yet! (%s)' % (format,enum))
     342                raise TypeError('WriteData error message: data type: {} not supported yet! ({})'.format(datatype,enum))
    381343        # }}}
    382344
    383 def FormatToCode(format): # {{{
     345def FormatToCode(datatype): # {{{
    384346        """
    385         This routine takes the format string, and hardcodes it into an integer, which
     347        This routine takes the datatype string, and hardcodes it into an integer, which
    386348        is passed along the record, in order to identify the nature of the dataset being
    387349        sent.
    388350        """
    389351
    390         if   m.strcmpi(format,'Boolean'):
     352        if datatype=='Boolean':
    391353                code=1
    392         elif m.strcmpi(format,'Integer'):
     354        elif datatype=='Integer':
    393355                code=2
    394         elif m.strcmpi(format,'Double'):
     356        elif datatype=='Double':
    395357                code=3
    396         elif m.strcmpi(format,'String'):
     358        elif datatype=='String':
    397359                code=4
    398         elif m.strcmpi(format,'BooleanMat'):
     360        elif datatype=='BooleanMat':
    399361                code=5
    400         elif m.strcmpi(format,'IntMat'):
     362        elif datatype=='IntMat':
    401363                code=6
    402         elif m.strcmpi(format,'DoubleMat'):
     364        elif datatype=='DoubleMat':
    403365                code=7
    404         elif m.strcmpi(format,'MatArray'):
     366        elif datatype=='MatArray':
    405367                code=8
    406         elif m.strcmpi(format,'StringArray'):
     368        elif datatype=='StringArray':
    407369                code=9
    408         elif m.strcmpi(format,'CompressedMat'):
     370        elif datatype=='CompressedMat':
    409371                code=10
    410372        else:
Note: See TracChangeset for help on using the changeset viewer.