Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Porting a sub-class of python2 'file' class to python3

I have a legacy code that calls class TiffFile(file). What is the python3 way to call it?

I tried to replace following in python2:

class TiffFile(file):
    def __init__(self, path):
        file.__init__(self, path, 'r+b')

By this in python3:

class TiffFile(RawIOBase):
    def __init__(self, path):
        super(TiffFile, self).__init__(path, 'r+b')

But now I am getting TypeError: object.__init__() takes no parameters

like image 730
Dima Lituiev Avatar asked Dec 15 '17 19:12

Dima Lituiev


People also ask

Can Python3 use Python2 modules?

You can't. Any module you import in py3 codebase needs to be py3 compatible.

Can I use python 2 and 3 together?

We can have both Python 2 and Python 3 installed on any Windows or Linux device. We can either create different environments on different IDEs to use the versions separately or use the following ways to run them using the command prompt.

How do I know if a script is python 2 or 3?

If you want to determine whether Python2 or Python3 is running, you can check the major version with this sys. version_info. major . 2 means Python2, and 3 means Python3.


1 Answers

RawIOBase.__init__ does not take any arguments, that is where you error is.

Your TiffFile implementation also inherits file which is not a a class, but a constructor function, so your Python 2 implementation is non-idiomatic, and someone could even claim it is wrong-ish. You should use open instead of file, and in a class context you should use an io module class for input and output.

You can use open to return a file object for use as you would use file in Python 2.7 or you can use io.FileIO in both Python 2 and Python 3 for accessing file streams, like you would do with open.

So your implementation would be more like:

import io

class TiffFile(io.FileIO):
    def __init__(self, name, mode='r+b', *args, **kwargs):
        super(TiffFile, self).__init__(name, mode, *args, **kwargs)

This should work in all currently supported Python versions and allow you the same interface as your old implementation while being more correct and portable.

Are you actually using r+b for opening the file in read-write-binary mode on Windows? You should probably be using rb mode if you are not writing into the file, but just reading the TIFF data. rb would open the file in binary mode for reading only. The appended + sets the file to open in read-write mode.

like image 176
Aleksi Häkli Avatar answered Oct 18 '22 13:10

Aleksi Häkli