From 738201c835b67f7ba4a88ac76885e96434ec2d3e Mon Sep 17 00:00:00 2001 From: zyd Date: Fri, 24 Feb 2023 23:26:20 +0900 Subject: [PATCH 1/4] unapply --- ramda/__init__.py | 1 + ramda/unapply.py | 10 ++++++++++ test/test_unapply.py | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 ramda/unapply.py create mode 100644 test/test_unapply.py diff --git a/ramda/__init__.py b/ramda/__init__.py index d60192f..622cd89 100644 --- a/ramda/__init__.py +++ b/ramda/__init__.py @@ -137,6 +137,7 @@ from .toString import toString from .toUpper import toUpper from .trim import trim +from .unapply import unapply from .unary import unary from .union import union from .unionWith import unionWith diff --git a/ramda/unapply.py b/ramda/unapply.py new file mode 100644 index 0000000..5dca89e --- /dev/null +++ b/ramda/unapply.py @@ -0,0 +1,10 @@ +from .private._curry1 import _curry1 + + +def _unapply(func): + def wrapper(*args): + return func(list(args)) + return wrapper + + +unapply = _curry1(_unapply) diff --git a/test/test_unapply.py b/test/test_unapply.py new file mode 100644 index 0000000..30e4f82 --- /dev/null +++ b/test/test_unapply.py @@ -0,0 +1,40 @@ + +import unittest + +import ramda as R +from ramda.private._inspect import funcArgsLength + +""" +https://github.com/ramda/ramda/blob/master/test/unapply.js +""" + + +class TestLt(unittest.TestCase): + def test_returns_a_function_which_is_always_passed_one_argument(self): + fn = R.unapply(lambda *args: len(args)) + + self.assertEqual(1, fn()) + self.assertEqual(1, fn('x')) + self.assertEqual(1, fn('x', 'y')) + self.assertEqual(1, fn('x', 'y', 'z')) + + def test_forwards_arguments_to_decorated_function_as_an_array(self): + fn = R.unapply(lambda xs: str(xs)) + + self.assertEqual('[]', fn()) + self.assertEqual('[2]', fn(2)) + self.assertEqual('[2, 4]', fn(2, 4)) + self.assertEqual('[2, 4, 6]', fn(2, 4, 6)) + + def test_returns_a_function_with_length_0(self): + fn = R.unapply(R.identity) + self.assertEqual(0, funcArgsLength(fn)) + + def test_is_the_inverse_of_R_apply(self): + f = R.Max + g = R.unapply(R.apply(f)) + self.assertEqual(f(1, 2, 3), g(1, 2, 3)) + + +if __name__ == '__main__': + unittest.main() From fb8766ccab37f3ffbb1f9ce84e8ce5020cef79e6 Mon Sep 17 00:00:00 2001 From: zyd Date: Fri, 24 Feb 2023 23:30:34 +0900 Subject: [PATCH 2/4] update pr-check python --- .github/workflows/code-coverage.yml | 2 +- .github/workflows/pr-check.yml | 2 +- .github/workflows/upload-to-pip.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/code-coverage.yml b/.github/workflows/code-coverage.yml index 63df5fa..cc460d4 100644 --- a/.github/workflows/code-coverage.yml +++ b/.github/workflows/code-coverage.yml @@ -23,7 +23,7 @@ jobs: - uses: actions/checkout@v2 # Sets up python3 - - uses: actions/setup-python@v2 + - uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index 1e24d3c..9af1f84 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -29,7 +29,7 @@ jobs: - uses: actions/checkout@v2 # Sets up python3 - - uses: actions/setup-python@v2 + - uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} diff --git a/.github/workflows/upload-to-pip.yml b/.github/workflows/upload-to-pip.yml index 32a5727..d6347ab 100644 --- a/.github/workflows/upload-to-pip.yml +++ b/.github/workflows/upload-to-pip.yml @@ -21,7 +21,7 @@ jobs: - uses: actions/checkout@v2 # Sets up python - - uses: actions/setup-python@v2 + - uses: actions/setup-python@v4 with: python-version: 3.x From ae53b61116601f6c0b6ab4c52ee66dcb54419ba9 Mon Sep 17 00:00:00 2001 From: zyd Date: Fri, 24 Feb 2023 23:34:10 +0900 Subject: [PATCH 3/4] try ubuntu-20.04 instead --- .github/workflows/code-coverage.yml | 2 +- .github/workflows/pr-check.yml | 2 +- .github/workflows/upload-to-pip.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/code-coverage.yml b/.github/workflows/code-coverage.yml index cc460d4..1ef3a65 100644 --- a/.github/workflows/code-coverage.yml +++ b/.github/workflows/code-coverage.yml @@ -13,7 +13,7 @@ on: jobs: build: - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 strategy: matrix: python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"] diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index 9af1f84..920d127 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -18,7 +18,7 @@ jobs: # This workflow contains a single job called "build" build: # The type of runner that the job will run on - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 strategy: matrix: python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"] diff --git a/.github/workflows/upload-to-pip.yml b/.github/workflows/upload-to-pip.yml index d6347ab..f5f6403 100644 --- a/.github/workflows/upload-to-pip.yml +++ b/.github/workflows/upload-to-pip.yml @@ -13,7 +13,7 @@ jobs: # This workflow contains a single job called "upload" upload: # The type of runner that the job will run on - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 # Steps represent a sequence of tasks that will be executed as part of the job steps: From 7461ef3952349cf3a4cce6a17b6457a723140afd Mon Sep 17 00:00:00 2001 From: zyd Date: Fri, 24 Feb 2023 23:41:51 +0900 Subject: [PATCH 4/4] fix lint --- ramda/compose.py | 2 +- ramda/concat.py | 6 +++--- ramda/map.py | 2 +- ramda/pipe.py | 2 +- ramda/private/_arity.py | 2 +- ramda/private/_assoc.py | 2 +- ramda/private/_createReduce.py | 2 +- ramda/private/_stepCat.py | 2 +- ramda/private/_xwrap.py | 2 +- 9 files changed, 11 insertions(+), 11 deletions(-) diff --git a/ramda/compose.py b/ramda/compose.py index 6970e20..43bbc0f 100644 --- a/ramda/compose.py +++ b/ramda/compose.py @@ -4,5 +4,5 @@ def compose(*arguments): if len(arguments) == 0: - raise Exception('compose requires at least one argument') + raise ValueError('compose requires at least one argument') return pipe(*reverse(arguments)) diff --git a/ramda/concat.py b/ramda/concat.py index d594567..2ae26f3 100644 --- a/ramda/concat.py +++ b/ramda/concat.py @@ -10,16 +10,16 @@ def inner_concat(a, b): if _isArray(a): if _isArray(b): return a + b - raise Exception(f"{toString(b)} is not an array") + raise ValueError(f"{toString(b)} is not an array") if _isString(a): if _isString(b): return a + b - raise Exception(f"{toString(b)} is not a string") + raise ValueError(f"{toString(b)} is not a string") if a is not None and _isFunction(getAttribute(a, 'fantasy-land/concat')): return a.get('fantasy-land/concat')(b) if a is not None and _isFunction(a.concat): return a.concat(b) - raise Exception(f'{a} does not have a method named "concat" or "fantasy-land/concat"') + raise ValueError(f'{a} does not have a method named "concat" or "fantasy-land/concat"') concat = _curry2(inner_concat) diff --git a/ramda/map.py b/ramda/map.py index 4ed4215..686230b 100644 --- a/ramda/map.py +++ b/ramda/map.py @@ -26,7 +26,7 @@ def inner_reduce(acc, key): return acc if functor is None: - raise Exception('Can not work with None') + raise ValueError('Can not work with None') if _isFunction(functor): return curryN(funcArgsLength(functor), lambda *arguments: fn(functor(*arguments))) if _isArrayLike(functor): diff --git a/ramda/pipe.py b/ramda/pipe.py index 80cb62b..76d89dc 100644 --- a/ramda/pipe.py +++ b/ramda/pipe.py @@ -7,7 +7,7 @@ def pipe(*arguments): if len(arguments) == 0: - raise Exception('pipe requires at least one argument') + raise ValueError('pipe requires at least one argument') return _arity( funcArgsLength(arguments[0]), reduce(_pipe, arguments[0], tail(list(arguments))) diff --git a/ramda/private/_arity.py b/ramda/private/_arity.py index baa083c..78a6bb5 100644 --- a/ramda/private/_arity.py +++ b/ramda/private/_arity.py @@ -18,4 +18,4 @@ def f10(a0=__, a1=__, a2=__, a3=__, a4=__, a5=__, a6=__, a7=__, a8=__, a9=__, *_ m = {0: f0, 1: f1, 2: f2, 3: f3, 4: f4, 5: f5, 6: f6, 7: f7, 8: f8, 9: f9, 10: f10} if n in m: return m[n] - raise Exception('First argument to _arity must be a non-negative integer no greater than ten') + raise ValueError('First argument to _arity must be a non-negative integer no greater than ten') diff --git a/ramda/private/_assoc.py b/ramda/private/_assoc.py index 8b8ac68..9fdf4e3 100644 --- a/ramda/private/_assoc.py +++ b/ramda/private/_assoc.py @@ -12,4 +12,4 @@ def _assoc(prop, val, obj): # We have 2 cases, dict or object if isinstance(obj, dict): return {**obj, prop: val} - raise Exception('We only support dict or array for assoc') + raise ValueError('We only support dict or array for assoc') diff --git a/ramda/private/_createReduce.py b/ramda/private/_createReduce.py index 9d4cc22..dcfd10c 100644 --- a/ramda/private/_createReduce.py +++ b/ramda/private/_createReduce.py @@ -17,5 +17,5 @@ def _reduce(xf, acc, arr): return iterableReduce(xf, acc, arr) if _isFunction(getattr(arr, 'reduce', None)): return methodReduce(xf, acc, arr, 'reduce') - raise Exception('reduce: list must be array or iterable') + raise ValueError('reduce: list must be array or iterable') return _reduce diff --git a/ramda/private/_stepCat.py b/ramda/private/_stepCat.py index 1faa214..4ff7cf3 100644 --- a/ramda/private/_stepCat.py +++ b/ramda/private/_stepCat.py @@ -38,4 +38,4 @@ def _stepCat(obj): return _stepCatString if isinstance(obj, dict): return _stepCatDict - raise Exception(f'Cannot create transformer for {obj}') + raise ValueError(f'Cannot create transformer for {obj}') diff --git a/ramda/private/_xwrap.py b/ramda/private/_xwrap.py index b2a363f..429e5b9 100644 --- a/ramda/private/_xwrap.py +++ b/ramda/private/_xwrap.py @@ -1,5 +1,5 @@ def transducer_init(): - raise Exception('init not implemented on XWrap') + raise ValueError('init not implemented on XWrap') def _xwrap(fn):