I have a working interview (yay!), and I'm working on refactoring it. I'm trying to figure out what might be a good way to move my code
blocks into .py
files. In particular, I'm worried about maintaining some of the fancy things docassemble does (the particular code
block I'm thinking about is marked initial: True
, for example).
Would the idea be to convert whatever your code
blocks are doing into functions, and then assign the variables docassemble
is looking for in a way that uses those functions? Presumably the results of those functions still need to be dealt with in code
blocks?
Is the below roughly the right approach (code below)? I'm assuming that:
- Trying to run
Bcode.func(A ...)
as below, ifA
was undefined, would trigger the exception necessary to causedocassemble
to search for the question needed to setA
? - Variables can be passed as set out below.
returning
from functions works as set out below.
Are any / all of these assumptions correct / incorrect?
So...
If this was my questions.yml
file:
---
question: A?
yesno: A
---
# code setting value of B
---
code: |
if A:
answer = "A"
elif B:
answer = "B"
else:
answer = "C"
---
To abstract away the code, I assume I might do something like this?
questions.yml
:
---
imports:
- Bcode
---
question: A?
yesno: A
---
initial: True
code: |
answer = Bcode.func(A, *args_to_set_value_of_B)
---
Bcode.py
:
---
def func(a, *args_to_set_value_of_b):
# code computing value of b
if a:
return "A"
elif b:
return "B"
else:
return "C"
---