11"""
22This is the file that contains the code to compile a given command on a model.
33
4- There are a few ways to compile the commands for a model, firstly if there is a command object already initialized
5- that has a valid compile key and a list of components the base method of `compile_command(command)` can be used to produce
6- the desired output. If no command object has been initialized then the `dynamic_compile(*components, compile_key=None)`
7- can be used to produce the desired output without the need to first go through a command object. The output of either
8- compile method will be the same.
4+ There are a few ways to compile the commands for a model, firstly if there is
5+ a command object already initialized that has a valid compile key and a list
6+ of components the base method of `compile_command(command)` can be used to
7+ produce the desired output. If no command object has been initialized then
8+ the `dynamic_compile(*components, compile_key=None)` can be used to produce
9+ the desired output without the need to first go through a command object. The
10+ output of either compile method will be the same.
911
1012The output produced by compiling a command will be two objects.
1113
12- The first object produced by compiling a command is the compiled method itself. This method requires at least one
13- positional argument and then any number of additional arguments. The first argument that is provided to the compiled
14- method is a python dictionary that contains the state for all compartments this method will need to access,
15- as discerning this can be a challenge it is normal to just pass it all compartments present on your model. The
16- remaining list of arguments are all the run time arguments that the various compiled methods need to run properly.
17- The return value of this compiled method is the final state of all compartments after running through the compiled
18- command. Note here that the value on the compartments are not automatically updated and that will need to be done after.
19-
20- The second object produced by compiling a command is the list of arguments that the compile command is expecting to
21- be passed in alongside the initial state of all the compartments. It is a good habit to get into printing this list
22- out after compiling as it can help catch typos present in the compiled methods that will not cause the compiling to
23- fail but will produce unknown behavior.
24-
25- There is a wrapper method offered in this file we recommend using to assist with the design patterned required by the
26- compiled command. This is done with `wrap_command(command)`. This method will return another method that removes the
27- need for creating the initial state of the compartments and setting all the compartment values after running. Arguments
28- are still required to be passed in at run time.
14+ The first object produced by compiling a command is the compiled method
15+ itself. This method requires at least one positional argument and then any
16+ number of additional arguments. The first argument that is provided to the
17+ compiled method is a python dictionary that contains the state for all
18+ compartments this method will need to access, as discerning this can be a
19+ challenge it is normal to just pass it all compartments present on your
20+ model. The remaining list of arguments are all the run time arguments that
21+ the various compiled methods need to run properly. The return value of this
22+ compiled method is the final state of all compartments after running through
23+ the compiled command. Note here that the value on the compartments are not
24+ automatically updated and that will need to be done after.
25+
26+ The second object produced by compiling a command is the list of arguments
27+ that the compile command is expecting to be passed in alongside the initial
28+ state of all the compartments. It is a good habit to get into printing this
29+ list out after compiling as it can help catch typos present in the compiled
30+ methods that will not cause the compiling to fail but will produce unknown
31+ behavior.
32+
33+ There is a wrapper method offered in this file we recommend using to assist
34+ with the design patterned required by the compiled command. This is done with
35+ `wrap_command(command)`. This method will return another method that removes
36+ the need for creating the initial state of the compartments and setting all
37+ the compartment values after running. Arguments are still required to be
38+ passed in at run time.
2939
3040"""
31- from ngcsimlib .compilers .component_compiler import parse as parse_component , compile as compile_component
41+ from ngcsimlib .compilers .component_compiler import parse as parse_component , \
42+ compile as compile_component
3243from ngcsimlib .compilers .op_compiler import parse as parse_connection
3344from ngcsimlib .utils import Get_Compartment_Batch , Set_Compartment_Batch
3445from ngcsimlib .logger import critical
3546
47+
3648def _compile (compile_key , components ):
3749 """
38- This is the top level compile method for commands. Note this does not actually require you to compile a
39- specific command object as it works purely off the compile key provided to the method.
40- The general process that this takes to compile down everything, is by producing an execution order that knows which
41- methods that are going to be called and where the results of the method are supposed to be stored.
50+ This is the top level compile method for commands. Note this does not
51+ actually require you to compile a specific command object as it works
52+ purely off the compile key provided to the method. The general process
53+ that this takes to compile down everything, is by producing an execution
54+ order that knows which methods that are going to be called and where the
55+ results of the method are supposed to be stored.
4256
4357 The execution order is as follows:
4458
@@ -47,8 +61,8 @@ def _compile(compile_key, components):
4761 | resolve the outputs of the compiled function
4862
4963 Args:
50- compile_key: The key that is being compiled (mapped to each function that has the @resolver decorator
51- above it)
64+ compile_key: The key that is being compiled (mapped to each function
65+ that has the @resolver decorator above it)
5266
5367 components: The list of components to compile for this function
5468
@@ -86,11 +100,17 @@ def _compile(compile_key, components):
86100 for c_name , component in components .items ():
87101 exc_order .extend (compile_component (component , resolvers [c_name ]))
88102
89- def compiled (compartment_values , ** kwargs ):
103+ def compiled (compartment_values = None , ** kwargs ):
104+ if compartment_values is None :
105+ critical (
106+ f"Attempting to call a compiled method without the current "
107+ f"state of the model. "
108+ f"Verify the method is wrapped or a current state is provided" )
90109 for n in needed_args :
91110 if n not in kwargs :
92- critical (f"Missing keyword argument \" { n } \" in compiled function."
93- f"\t Expected keyword arguments { needed_args } " )
111+ critical (
112+ f"Missing keyword argument \" { n } \" in compiled function."
113+ f"\t Expected keyword arguments { needed_args } " )
94114
95115 for exc , outs , name , comp_ids in exc_order :
96116 _comps = {key : compartment_values [key ] for key in comp_ids }
@@ -107,7 +127,8 @@ def compiled(compartment_values, **kwargs):
107127
108128def compile_command (command ):
109129 """
110- Compiles a given command object to the spec described at the top of this file
130+ Compiles a given command object to the spec described at the top of this
131+ file
111132
112133 Args:
113134 command: the command object
@@ -121,8 +142,8 @@ def compile_command(command):
121142
122143def dynamic_compile (* components , compile_key = None ):
123144 """
124- Dynamically compiles a command without the need of a command object to produce
125- the spec described at the top of this file.
145+ Dynamically compiles a command without the need of a command object to
146+ produce the spec described at the top of this file.
126147
127148 Args:
128149 *components: a list of components to be compiled
@@ -149,6 +170,7 @@ def wrap_command(command):
149170 Returns:
150171 the output of the command after it's been executed
151172 """
173+
152174 def _wrapped (** kwargs ):
153175 vals = command (Get_Compartment_Batch (), ** kwargs )
154176 Set_Compartment_Batch (vals )
0 commit comments