Skip to content

Commit 1ce357d

Browse files
authored
Op movement (#11)
* Move operations into their own directory * Fix kwarg problem in wrapping commands
1 parent 86e5ea2 commit 1ce357d

File tree

9 files changed

+74
-58
lines changed

9 files changed

+74
-58
lines changed

ngcsimlib/compilers/command_compiler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ def wrap_command(command):
148148
Returns:
149149
the output of the command after it's been executed
150150
"""
151-
def _wrapped(*args):
152-
vals = command(Get_Compartment_Batch(), *args)
151+
def _wrapped(**kwargs):
152+
vals = command(Get_Compartment_Batch(), **kwargs)
153153
Set_Compartment_Batch(vals)
154154
return vals
155155

ngcsimlib/compilers/op_compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
as the `add` operation are not compilable as their resolve method contains execution logic that will not be captured
1111
by the compiled command.
1212
"""
13-
from ngcsimlib.operations import BaseOp
13+
from ngcsimlib.operations.baseOp import BaseOp
1414

1515

1616
def parse(op):

ngcsimlib/context.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from ngcsimlib.logger import warn, info
33
from ngcsimlib.utils import get_compartment_by_name, \
44
get_context, add_context, get_current_path, get_current_context, set_new_context
5-
from ngcsimlib.compilers.command_compiler import dynamic_compile
5+
from ngcsimlib.compilers.command_compiler import dynamic_compile, wrap_command
66
import json, os
77

88

@@ -343,7 +343,7 @@ def make_commands(self, path_to_commands_file):
343343
commands = json.load(file)
344344
for c_name, command in commands.items():
345345
if command['class'] == "dynamic_compiled":
346-
self.compile_command_key(
346+
self.compile_by_key(
347347
*self.get_components(*command['components']), compile_key=command['compile_key'], name=c_name)
348348
else:
349349
klass = load_from_path(command['class'])
@@ -384,7 +384,7 @@ def dynamicCommand(fn):
384384
get_current_context().__setattr__(fn.__name__, fn)
385385
return fn
386386

387-
def compile_command_key(self, *components, compile_key, name=None):
387+
def compile_by_key(self, *components, compile_key, name=None):
388388
"""
389389
Compiles a given set of components with a given compile key.
390390
It will automatically add it to the context after compiling
@@ -408,3 +408,15 @@ def compile_command_key(self, *components, compile_key, name=None):
408408
self._json_objects['commands'][name] = {"class": klass, "components": _components, "compile_key": compile_key}
409409
self.__setattr__(name, cmd)
410410
return cmd, args
411+
412+
413+
def wrap_and_add_command(self, command, name=None):
414+
"""
415+
wraps a command and adds it to the context, if no name is provided it will use the command's internal name
416+
417+
Args:
418+
command: The command to wrap
419+
420+
name: The name of the command (default: None)
421+
"""
422+
self.add_command(wrap_command(command), name=name)

ngcsimlib/operations/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .baseOp import BaseOp
2+
from .add import add
3+
from .negate import negate
4+
from .overwrite import overwrite
5+
from .summation import summation

ngcsimlib/operations/add.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from .summation import summation
2+
3+
class add(summation):
4+
"""
5+
Not Compilable
6+
7+
A subclass of summation that also adds the destinations value instead of overwriting it. For a compiler friendly
8+
version of this add the destination as a source to summation.
9+
"""
10+
is_compilable = False
11+
12+
def resolve(self, value):
13+
if self.destination is not None:
14+
self.destination.set(self.destination.value + value)
15+
16+
Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from abc import abstractmethod, ABC
22

3-
43
class BaseOp(ABC):
54
"""
65
This is the base class for all operations that define cable behavior in ngcsimlib. Generally the there is one
@@ -85,54 +84,4 @@ def __repr__(self) -> str:
8584
line += f"\tSources: {[source for source in self.sources]}"
8685
if self.destination is not None:
8786
line += f"\tDestination: {self.destination.name}"
88-
return line
89-
90-
91-
class summation(BaseOp):
92-
"""
93-
Adds together all the provided compartment's values and overwrites the previous value
94-
"""
95-
96-
@staticmethod
97-
def operation(*sources):
98-
s = None
99-
for source in sources:
100-
if s is None:
101-
s = source
102-
else:
103-
s += source
104-
return s
105-
106-
107-
class negate(BaseOp):
108-
"""
109-
negates the first source compartment (other will be ignored) and overwrite the previous value
110-
"""
111-
112-
@staticmethod
113-
def operation(*sources):
114-
return -sources[0]
115-
116-
117-
class add(summation):
118-
"""
119-
Not Compilable
120-
121-
A subclass of summation that also adds the destinations value instead of overwriting it. For a compiler friendly
122-
version of this add the destination as a source to summation.
123-
"""
124-
is_compilable = False
125-
126-
def resolve(self, value):
127-
if self.destination is not None:
128-
self.destination.set(self.destination.value + value)
129-
130-
131-
class overwrite(BaseOp):
132-
"""
133-
The default operation behavior for cable's
134-
Overwrites the previous value with the first source value (all other sources will be ignored)
135-
"""
136-
@staticmethod
137-
def operation(*sources):
138-
return sources[0]
87+
return line

ngcsimlib/operations/negate.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from .baseOp import BaseOp
2+
class negate(BaseOp):
3+
"""
4+
negates the first source compartment (other will be ignored) and overwrite the previous value
5+
"""
6+
7+
@staticmethod
8+
def operation(*sources):
9+
return -sources[0]

ngcsimlib/operations/overwrite.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from .baseOp import BaseOp
2+
class overwrite(BaseOp):
3+
"""
4+
The default operation behavior for cable's
5+
Overwrites the previous value with the first source value (all other sources will be ignored)
6+
"""
7+
@staticmethod
8+
def operation(*sources):
9+
return sources[0]

ngcsimlib/operations/summation.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from .baseOp import BaseOp
2+
3+
class summation(BaseOp):
4+
"""
5+
Adds together all the provided compartment's values and overwrites the previous value
6+
"""
7+
8+
@staticmethod
9+
def operation(*sources):
10+
s = None
11+
for source in sources:
12+
if s is None:
13+
s = source
14+
else:
15+
s += source
16+
return s

0 commit comments

Comments
 (0)