0

I have my code here that reads my config file:

def getConfig(env):
    pwd=os.getcwd()
    if "win" in (platform.system().lower()):
        f = open(pwd+"\config_"+env.lower()+"_dataset2.json")
    else:
        f = open(pwd+"/config_"+env.lower()+"_dataset2.json")
    config = json.load(f)
    f.close()
    return config

The thing is, I want it to read my other config file as well but I don't know how to incorporate it in the original code above. I know something like this won't work:

def getConfig(env):
    pwd=os.getcwd()
    if "win" in (platform.system().lower()):
        f = open(pwd+"\config_"+env.lower()+"_dataset2.json")
    else:
        f = open(pwd+"/config_"+env.lower()+"_dataset2.json")
    if "win" in (platform.system().lower()):
        f = open(pwd+"\config_"+env.lower()+"_dataset1.json")
    else:
        f = open(pwd+"/config_"+env.lower()+"_dataset1.json")
    config = json.load(f)
    f.close()
    return config

I'm stuck on having the option to run both config files at once, or just run dataset1.json individually, or just run dataset2.json individually.

Maybe something like:

dataset2=config_"+env.lower()+"_dataset2.json
dataset1=config_"+env.lower()+"_dataset1.json

if dataset2:
          f = open(pwd+"\config_"+env.lower()+"_dataset2.json")....


@staticmethod
def getConfig(env):
    pwd=os.getcwd()
    env = env.lower()

    with open(os.path.join(pwd, f"config_{env}_dataset2.json")) as f:
        config2 = json.load(f)
    with open(os.path.join(pwd, f"config_{env}_dataset1.json")) as f:
        config1 = json.load(f)

    return config2, config1

config2, config1 = getConfig(env)

TypeError: 'staticmethod' object is not callable
  • 1
    You should use a raw string if the string contains a literal backslash. – Barmar Jan 12 '23 at 21:55
  • 2
    But why do you need to do this? Windows allow you to use `/` as the directory separator. You can also use `pathlib` and let it deal with the OS dependencies. – Barmar Jan 12 '23 at 21:56

1 Answers1

3

You can return multiple values from the function.

There's no need for the conditionals, use os.path.join() to combine pathnames with the OS-specific delimiter.

def getConfig(env):
    pwd=os.getcwd()
    env = env.lower()

    with open(os.path.join(pwd, f"config_{env}_dataset2.json")) as f:
        config1 = json.load(f)
    with open(os.path.join(pwd, f"config_{env}_dataset1.json")) as f:
        config2 = json.load(f)

    return config1, config2

dataset1, dataset2 = getConfig(env)

You also don't really need to join with pwd, since relative pathnames are interpreted relative to the current directory.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • thanks for the response. i have tried your code and it doesn't work because (i forgot to include) @staticmethod above my original function. it returns the error: TypeError: 'staticmethod' object is not callable – noride_togo Jan 13 '23 at 00:47
  • https://stackoverflow.com/questions/41921255/staticmethod-object-is-not-callable – Barmar Jan 13 '23 at 00:49
  • hmm im a little confused because im not storing anything in a dictionary – noride_togo Jan 13 '23 at 01:21
  • `@staticmethod` has to be inside a class. – Barmar Jan 13 '23 at 01:25
  • Then you call it with `className.getConfig(env)` or `classInstance.getConfig(env)` – Barmar Jan 13 '23 at 01:26
  • That means that either `self.config` or `self.config["data-validation"]` is a list, not a dictionary. – Barmar Jan 13 '23 at 01:37
  • Yes, that's what it means. And the problem is that it's being used to index a tuple, not a dictionary. – Barmar Jan 13 '23 at 01:46
  • How are you setting `self.config`? Did you use `self.config = self.getConfig(env)`? My answer makes `getConfig()` return a tuple -- `self.config[0]` comes from `dataset2.json`, `self.config[1]` is from `dataset1.json`. – Barmar Jan 13 '23 at 01:47
  • And if you're using the version of `getConfig()` from my answer, it returns 2 configurations. You need to index them. Did you want them to be merged in some way? – Barmar Jan 13 '23 at 01:54