Skip to content

Commit

Permalink
actionpack#79: "enhances pipeline.fitting param flexibility" (#83)
Browse files Browse the repository at this point in the history
* adds Pipeline.Receiver test

* adds utils requisite for implementation

* adds and uses Pipeline.Receive implementation

* fixes PipelineTest
  • Loading branch information
withtwoemms authored Oct 20, 2021
1 parent b68e65a commit 33a6fba
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
12 changes: 12 additions & 0 deletions actionpack/actions/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from actionpack import Action
from actionpack.action import ActionType
from actionpack.actions import Call
from actionpack.utils import first
from actionpack.utils import key_for
from actionpack.utils import swap


class Pipeline(Action):
Expand Down Expand Up @@ -36,6 +39,12 @@ def flush(self, given_action: Optional[Action] = None) -> Action:
params_dict = OrderedDict(signature(next_action_type.action.__init__).parameters.items())
params_dict.pop('self', None)
params = list(params_dict.keys())
conduit = first(params)
if conduit in next_action_type.kwargs and key_for(Pipeline.Receiver, next_action_type.kwargs):
params = swap(
params, 0,
params.index(key_for(Pipeline.Receiver, next_action_type.kwargs))
)
keyed_result = dict(zip(params, [keyed_result['action']]))
next_action_type.kwargs.update(keyed_result)
next_action = next_action_type.action(**next_action_type.kwargs)
Expand All @@ -55,6 +64,9 @@ def __next__(self):
except StopIteration:
self._action_types = iter(self._action_types)

class Receiver:
pass

class Fitting(type):

@staticmethod
Expand Down
17 changes: 16 additions & 1 deletion actionpack/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import pickle

from functools import wraps
from typing import Callable, Iterable
from typing import Callable
from typing import Dict
from typing import Generic
from typing import Iterable
from typing import List
from typing import Optional
from typing import TypeVar

Expand Down Expand Up @@ -64,3 +67,15 @@ def first(iterable: Iterable):

def last(iterable: Iterable):
return iterable[-1]


def key_for(value: T, dct: Dict) -> Optional[T]:
try:
return list(dct.keys())[list(dct.values()).index(value)]
except ValueError:
return None


def swap(lst: List, idx1: int, idx2: int) -> List:
lst[idx1], lst[idx2] = lst[idx2], lst[idx1]
return lst
30 changes: 29 additions & 1 deletion tests/actionpack/actions/test_pipeline.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from os import getcwd as cwd
from unittest import TestCase
from unittest.mock import patch

Expand Down Expand Up @@ -34,7 +35,7 @@ def test_Pipeline(self, mock_input, mock_exists, mock_file):
@patch('pathlib.Path.open')
@patch('pathlib.Path.exists')
@patch('builtins.input')
def test_Pipeline_FittingType(self, mock_input, mock_exists, mock_file):
def test_Pipeline_Fitting(self, mock_input, mock_exists, mock_file):
filename = 'this/file.txt'
file = FakeFile()
question = b'How are you?'
Expand All @@ -60,3 +61,30 @@ def test_Pipeline_FittingType(self, mock_input, mock_exists, mock_file):
self.assertEqual(file.read(), question + b'\n')
self.assertIsInstance(result, Result)
self.assertEqual(result.value, reply)

@patch('pathlib.Path.open')
@patch('pathlib.Path.exists')
@patch('builtins.input')
def test_Pipeline_Receiver(self, mock_input, mock_exists, mock_file):
filename = 'this/file.txt'
file = FakeFile()
reply = b"I'd like to record this."

mock_file.return_value = file
mock_exists.return_value = True
mock_input.return_value = reply

read_input = ReadInput('What would you like to record?')
action_types = [
Pipeline.Fitting(
action=Write,
reaction=Call(Closure(bytes.decode)),
**{'append': True, 'filename': filename, 'to_write': Pipeline.Receiver},
)
]
pipeline = Pipeline(read_input, *action_types, should_raise=True)
result = pipeline.perform(should_raise=True)

self.assertEqual(file.read(), reply + b'\n')
self.assertIsInstance(result, Result)
self.assertEqual(result.value, f'{cwd()}/{filename}')

0 comments on commit 33a6fba

Please sign in to comment.