Question

I am automating an environment that consists of multiple independent applications. At some point I have decided that it will make the most sense to define each application as a class and save it as a separate file.

What I have at the moment:

A directory with *py files where each file defines a single class for a single application. So, for example, I have application App1. It is saved in App1.py file and looks something like this:

class App1:
    def __init__(self):
        self.link = config.App1_link
        self.retry = config.App1_retry
    def access(self):
        pass
    def logOut(self):
        pass

So each class defines all operations App1 can perform, like start\login to the application, perform operation, log out from the application, etc..

Then, in a separate file I create application objects and call object's methods one by one to create a multiple-steps scenario.

What I want to achieve

It all works fine but there is a place for improvement. Many of the class methods are more or less similar (for example methods like login\logout to\from the application) and clearly could be defined in some kind of base class I can then inherit from. However, since I have all the applications defined in separate files (which seems the logical solution at the moment), I don't know where to define such base class and how to inherit from it. I had 2 options in mind but I'm not sure which one (if any) will be the proper way to do it.

  1. Define a base class in a separate file and then import it into each one of the application classes. Not sure if that's even makes sense.
  2. Create one very long file with all the applications classes and include the base class in the same file. Note: such file will be really huge and hard to maintain since each application has its own methods and some of them a fairly big files by themselves.

So are these the only options I have and if they are, which one of them makes more sense?

Was it helpful?

Solution

This is exactly what importing is for. Define your base class in a separate module, and import it wherever you need it. Option 1 makes excellent sense.

Note that Python's module naming convention is to use lowercase names; that way you can distinguish between the class and the module much easier. I'd rename App1.py to app1.py.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top