I don't really understand this error, do you mean there are two arguments?

Asked 2 years ago, Updated 2 years ago, 122 views

#!/usr/bin/env python

import numpy
importos
import structure
import sys

class STF:
    def__init__(self):
        self.endian='>'
        self.chunks=['APSG']

    defloadfile(self, filename):
        with open(RESULT20191219.txt, 'rb') asstf_file:
            self.load(stf_file)

    defload(self, stf_file):
        filesize=os.fstat(stf_file.fileno()) .st_size

        while stf_file.tell()<filesize:
            chunk=stf_file.read(4)

            if chunk == 'STRT':
                if stf_file.read(2) == '\xff\xfe':
                    self.endian='<'
                chunk_size, self.version, self.channel, self.frequency=struct.unpack(self.endian+'IHHI', stf_file.read(12))
            else:
                chunk_size, =struct.unpack(self.endian+'I', stf_file.read(4))

                if chunk=='CHKL' or chunk=='NXFL':
                    data=stf_file.read(chunk_size)
                    if chunk == 'CHKL':
                        self.chunks+ = [data[i:i+4] for i in range(0, chunk_size, 4) if data [i:i+4] not in self.chunks]
                else:
                    self.shift_length, frame_count, argument, self.bit_size, self.weight, data_size=struct.unpack(self.endian+'dIIHdI', stf_file.read(30))
                    data=stf_file.read(data_size)

                    element=data_size/(self.bit_size/8)
                    matrix=numpy.fromstring(data, count=element)

                    for cinself.chunks:
                        if chunk == c:
                            if element/frame_count ==1:
                                self.__dict__[c.trip()] = matrix
                            else:
                                self.__dict__[c.trip()] = matrix.reshape((frame_count, element/frame_count))
                            break

        for cinself.chunks:
            if c. strip() not in self.__dict__:
                self.__dict__[c.trip()] = None

    def savefile(self, filename):
        with open(filename, 'wb') as stf_file:
            self.save(stf_file)

    def save(self, stf_file):
        stf_file.write('STRT')
        if self.endian=='>':
            stf_file.write('\xfe\xff')
        elif self.endian=='<':
            stf_file.write('\xff\xfe')
        stf_file.write(struct.pack(self.endian+'IHI', 8, self.version, self.channel, self.frequency))

        stf_file.write('CHKL')
        stf_file.write(struct.pack(self.endian+'I',len(''.join(self.chunks))))))+'.join(self.chunks)))

        for cinself.chunks:
            if self.__dict__[c.trip()] is None:
                continue

            matrix=self.__dict__[c.trip()]
            iflen(matrix.shape) == 1:
                argument = 1
            else:
                argument=matrix.shape[1]
            data_size=matrix.shape[0]*argument*8

            header=structure.pack(self.endian+'dIIHdI', self.shift_length, matrix.shape[0], argument, self.bit_size, self.weight, data_size)
            stf_file.write(c+struct.pack(self.endian+'I',len(header)+data_size)+header)

            for i in xrange (matrix.shape[0]):
                if argument == 1:
                    stf_file.write(struct.pack(self.endian+'d', matrix[i]))
                else:
                    for jin xrange (matrix.shape[1]):
                        stf_file.write(struct.pack(self.endian+'d', matrix[i,j]))

if__name__=='__main__':
    iflen(sys.argv)<2:
        print 'Usage: %s<stf_file>'%sys.argv[0]
        sys.exit()

    stf = STF (sys.argv[1])
    print stf.F0
TypeError Traceback (most recent call last)
<ipython-input-5-5827b493a099>in<module>()
      4sys.exit()
      5 
---->6stf = STF (sys.argv[1])
      7 print stf.F0

TypeError: __init__() takes exactly 1 argument (2given)

Run is jupyter notebook.There was no error when I tried pycharm

python python2

2022-09-30 17:54

1 Answers

When you write STF(sys.argv[1]) to create an instance of class STF, the _init__ method of STF is called _init___(self,sys.argv[1]) during internal processing.There are two arguments given here, but _init__ in STF expects one argument, so this error occurs.

def_init__(self):
    self.endian='>'
    self.chunks=['APSG']


2022-09-30 17:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.