-1

Scenario: I have a file project.py whose job is to navigate to another folder to get the environment variables stored in a .yml file. There are two cases here, one job is to navigate to a directory and another to navigate to a directory in an egg file for which I've used zipfile module. How do I include both the cases in project.py and write an if else condition to pick between those two functions based on the directory type. Below is the code. I'm fairly new to programming, would greatly appreciate any help.

project.py

Class Proj(dict):

def __init__(self, envmt, app_path):

        self.config_dict = {}
        self.envmt = envmt
        with zipfile.Zipfile("C:/Users/project/poc/dist/project-1.0.0-py3.6", 'r') as myzip:
            config = load(myzip.open("base.yml_path"))
        for key, value in config.items():
             configvals = value
        for key in configvals:
             self.config_dict[key]= configvals[key]

        self.base = config
        with zipfile.Zipfile("C:/Users/project/poc/dist/project-1.0.0-py3.6", 'r') as myzip:
            config = load(myzip.open("env.yml_path"))

        for key, value in config.items():
             configvals = value
        for key in configvals:
             self.config_dict[key]= configvals[key]
        self.env = config

def __init__(self, envmt, app_path):

        self.config_dict = {}
        self.envmt = envmt
        with open(os.path.join(app_path, 'config', 'base.yml'), 'r') as cfile:
            config = load(cfile)
        for key, value in config.items():
             configvals = value
        for key in configvals:
             self.config_dict[key]= configvals[key]

        self.base = config
        with open(os.path.join(app_path, 'config', envmt+'.yml'), 'r') as cfile:
            config = load(cfile)

        for key, value in config.items():
             configvals = value
        for key in configvals:
             self.config_dict[key]= configvals[key]
        self.env = config

How can I write a condition to choose between the above two functions.

Community
  • 1
  • 1
Python
  • 1
  • 1
  • you write one `__init__(self, .....)` and inside it you check what to do. If you follow oop you might want to build an abstract class and derive two subclasses if your classes do other stuff as well. Or simply provide the name of your path/file to your function and if its a ZIP do _this_ else do _that_ – Patrick Artner Dec 16 '19 at 20:08
  • You are duplicating a lot of code here. I didn't read through everything, but I only saw about one or two lines that actually differed. perhaps you just need an if statement inside a single `__init__` – user1558604 Dec 16 '19 at 20:14
  • @user1558604 yeah was actually thinking about that, any suggestions on how to do it in a single __init__? – Python Dec 16 '19 at 20:20
  • @PatrickArtner I've just tried implementing that, although it's not a zip that I'm navigating to, it still takes the zipfile case since it is at the beginning of the #__init__ function. – Python Dec 16 '19 at 20:45
  • from the code you posted its not clear where the problem is. Simplify it: create a class that takes a path to a file and decide on the paths`s extension what part of your single ` __init__` should be executed.Simpliffying problems helps debugging them - and makes a better [mre] showing us your problem – Patrick Artner Dec 17 '19 at 07:57
  • There is more curious stuff in your code - f.e. whatfor are the loops `for key, value in config.items(): configvals = value` - they will loop through all items and only take the last one and feed it to your next loop iterating over all keys of `configvals` .. you really should [edit] your example into something simpler – Patrick Artner Dec 17 '19 at 08:13

1 Answers1

0

If both __init__s belong to the same class then only the last one will be used, consider writing a class like this:

class AClass:
    def __init__(self, envmt, app_path):
        if condicion == True:
            self._init_a(envmt, app_path)
        else:
            self._init_b(envmt, app_path)

    def _init_a(self, envmt, app_path):
        # run code here

    def _init_b(self, envmt, app_path):
        # run code here
marcos
  • 4,473
  • 1
  • 10
  • 24