3
  1. MainLibrary.py - All the important methods are available in this file
  2. SecondaryLibrary.py - specific methods are available in this file, which cannot be placed in the MainLibrary.py file

There are old scripts which only imports the MainLibrary and does not import the SecondayLibrary file. Here, when these old scripts are called - instead of accessing the methods from the mainlibrary file, is it possible to access the methods from the secondaryLibrary file without changing anything in the scripts or MainLibrary file.

Example:

MainLibrary.py file:

class MainLibrary:
    def x(self, a =0, b=0, c= 0):
        """ Do some operation with these values"""
    def y(self, a=0,b=0,c=0):
    """Do some operation with these values"""
    

SecondaryLibrary.py file

class SecondaryLibrary:
    def xy(self, a=0, b=0, c=0):
        """Compute the value of C based on the values of a and b and then do some operation"""

    

"The old scripts will recieve the values for paramters "a and b" and C will be always 0" But, with the new requirements, i need to compute the value of C based on the values of a and b - all the computation part are handled in xy method"

Note: I dont have permission to edit the MainLibrary file or the Scripts, everything has to be handled in the SecondaryLibrary file

Script:

from MainLibrary import *
obj = MainLibrary()
"get the values of a and b"
obj.x(a,b)  
Here when method X is called --> i need to call method "xy" from the sceondaryLibrary file.
    
NewLearner
  • 41
  • 2
  • Does this answer your question? [What is a Pythonic way for Dependency Injection?](https://stackoverflow.com/questions/31678827/what-is-a-pythonic-way-for-dependency-injection) – Adam Jun 29 '20 at 11:12
  • Do you control the pythonpath environment variable of the script? Or anything else about the script, for that matter? – Roy2012 Jun 29 '20 at 11:14
  • I believe this is a duplicate of [this question on dependency injection](https://stackoverflow.com/questions/31678827/what-is-a-pythonic-way-for-dependency-injection) and specifically [this answer](https://stackoverflow.com/a/61155318/1281548) directly solves your posed problem. – Adam Jun 29 '20 at 11:14
  • Sorry i am new to python and i am finding very difficult to understand the solution. Is there any other reference to the solution – NewLearner Jun 29 '20 at 11:38
  • @Adam eh, that's really not doing it "without importing", that's just *importing dynamically*. And you don't need to do that to add methods to a user-defined class, i.e. monkey-patching (the question is also confusingly about dependency injection, which really isn't relevant here, the answer isn't really dependency injection either). – juanpa.arrivillaga Jun 29 '20 at 11:42
  • @juanpa.arrivillaga I believe this is a question on dependency injection because of this; _Here when method X is called --> i need to call method "xy" from the sceondaryLibrary file._. I'll compose an answer to explore the idea. – Adam Jun 29 '20 at 12:28
  • Okay to be clear, when the test script calls method "x" --> Instead of executing the commands present in method "X", Method "xy" has to be executed from the secondaryLibrary file – NewLearner Jun 29 '20 at 13:15

1 Answers1

0

You can construct a class in your script to do what you require;

# MainLibrary.cs
class MainLibrary(object):

    def x(self, a, b, c):
        return a + b + c

    def y(self, a, b, c):
        return a * b * c

# SecondaryLibrary.cs
class SecondaryLibrary(object):

    def xy(self, a, b,c ):
        return c - a - b

# Script.cs
from MainLibraryimport MainLibrary
from SecondaryLibrary import SecondaryLibrary

class Script(MainLibrary, SecondaryLibrary):

    def x(self, a, b, c):
        # As in your question this is how we specify that calling
        # obj.x should return the result of .xy
        return super().xy(a, b, c)

    def y(self, a, b, c):
        return super().y(a, b, c)

if __name__ == '__main__':
    obj = Script()
    result = obj.x(1, 2, 5)
    print(result) # gives 5 - 2 - 1 => 2
Adam
  • 4,180
  • 2
  • 27
  • 31
  • Okay as i mentioned in the beginning I dont have permission to edit the script - I can do only changes in the secondaryLibrary.py file – NewLearner Jun 29 '20 at 13:22
  • @NewLearner, I don't believe what you're asking for is possible. – Adam Jun 29 '20 at 13:25