0

I have a file allthetests.py with a number of small functions defined like this:

def test1():
    ##do some stuff
    if (everything_is_awesome):
        return({"status":PASS, "data":"all the data"})

def test2():
    ##do some different stuff
    if (everything_is_bad):
        return({"status":FAIL, "data":"no data"})

Then I have another bunch of files which are descriptions of nodes and get imported as dicts like this

{"node_type":"action",
"title":"things",
"test":"test2"}

Finally I have a third file main.py which is in charge of everything. In this third file, I want to import allthetests.py, load the node descriptions and then call allthetests.test1() or allthetests.test2() or whatever that case is. I imagine it might look something like this but I'm not really sure where to go from here...

import allthetests

cur_node = load_node()

## Doesn't work but...
return_val = allthetests.cur_node['test']()
Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144
Indigo
  • 962
  • 1
  • 8
  • 23
  • 1
    If you can import `allthetests` into the files defining the dicts, you can just leave the quotes off like `"test":test2`. Functions are objects in Python. – Mark Ransom Oct 10 '17 at 23:02
  • @MarkRansom how would I go about actually running that function? Would it be just `return_val = curnode['test']` ? – Indigo Oct 10 '17 at 23:07
  • 1
    @Indigo that plus `()` to pass in params. So: `return_val = cur_node['test']()`. Alternately you can do `getattr(allthetests, cur_node)()` – Slater Victoroff Oct 10 '17 at 23:09

0 Answers0