Python, doing conditional imports the right way
Asked Answered
P

4

15

Right now I have a class called A.

I have some code like this..

from my.package.location.A import A

...


foo = A.doSomething(bar)

This is great.

But now I have a new version of A called A, but in a different package, but I only want to use this other A in a certain scenario. So I can do something like this:

if(OldVersion):
    from my.package.location.A import A
else:
    from new.package.location.A import A

...

foo = A.doSomething(bar)

This works fine. But it is ugly. How can I do this better? I really want to do something like this

from my.abstraction.layer.AFactory import AFactory
...
myA = AFactory.giveMeA() # this looks at "OldVersion" and gives me the correct A
foo = myA.doSomething(bar)

is there a way I can do that easier? Without the factory layer? This now can turn every static method call on my class into 2 lines. I can always hold a reference in a class to reduce the impact, but im really hoping python has a simpler solution.

Parasitology answered 22/7, 2011 at 17:25 Comment(0)
G
24

Put your lines into a_finder.py:

if OldVersion:
    from my.package.location.A import A
else:
    from new.package.location.A import A

Then in your product code:

from a_finder import A

and you will get the proper A.

Gaff answered 22/7, 2011 at 18:25 Comment(1)
It is too old an answer, but. How do I pass the parameter OldVersion to the file a_finder?Bureau
D
2

You could do something like this:

AlwaysRightA.py

import sys
if(OldVersion):
    from my.package.location.A import A
else:
    from new.package.location.A import A
sys.modules[__name__] = A

Then simply import AlwaysRightA as A and you're set.

Demavend answered 22/7, 2011 at 17:36 Comment(0)
B
1

Could you just make a package in some third location that checks OldVersion and gets it's own A from the right place, then always import that package?

Bordello answered 22/7, 2011 at 17:33 Comment(0)
A
0

handled a similar case like this

converted

if next_args['current_branch'] == "first_request":
    from .first_request import upload_data
elif next_args['current_branch'] == "get_address":
    from .get_address import upload_data
elif next_args['current_branch'] == "final_request":
    from .final_request import upload_data
else:
    raise ValueError(f'invalid value in postscript {next_args["current_branch"]}')

return upload_data(oc, next_args)

to

  def process_data(self) -> str:
        branch_mapping = {
            "first_request": ".first_request",
            "get_address": ".get_address",
            "final_request": ".final_request"
        }
    
        current_branch = self['current_branch']
        if current_branch in branch_mapping:
            module_name = branch_mapping[current_branch]
            upload_data = __import__(module_name, fromlist=['upload_data'])
            upload_data.upload_data()
        else:
            raise ValueError('Invalid value ')
Arak answered 8/8, 2023 at 6:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.