diff --git a/README.md b/README.md index 71c75bf..28185e9 100644 --- a/README.md +++ b/README.md @@ -427,7 +427,7 @@ Python modulo on negative numbers has different behavior than JS. ``` ```js -5 % -3 // 2 +5 % -3; // 2 ``` - [ ] move @@ -600,6 +600,8 @@ class A: def __init__(self, v2): self.v2 = v2 +R.toPairs(A(1)) # [['v2', 1]] + class B(A): v3 = 'not included' def __init__(self, v2, v4): @@ -610,7 +612,27 @@ b = B('v2', 'v4') R.toPairs(b) # [['v2', 'v2'], ['v4', 'v4']] ``` -- [ ] toPairsIn +- [x] toPairsIn + +```python +R.toPairsIn({'a': 1, 'b': 2}) # [['a', 1], ['b', 2]] + +class A: + v1 = 'included' + def __init__(self, v2): + self.v2 = v2 + +R.toPairsIn(A('v2')) # [['v1', 'included'], ['v2', 'v2']] + +class B(A): + v3 = 'included too' + def __init__(self, v2, v4): + super().__init__(v2) # this is required + self.v4 = v4 + +R.toPairsIn(B('v2', 'v4')) # [['v3', 'included too'], ['v1', 'included'], ['v2', 'v2'], ['v4', 'v4']] +``` + - [x] 0.1.2 toString Partially supported diff --git a/ramda/__init__.py b/ramda/__init__.py index 63ac60e..93bae61 100644 --- a/ramda/__init__.py +++ b/ramda/__init__.py @@ -107,6 +107,7 @@ from .tap import tap from .times import times from .toPairs import toPairs +from .toPairsIn import toPairsIn from .toString import toString from .unary import unary from .union import union diff --git a/ramda/toPairsIn.py b/ramda/toPairsIn.py new file mode 100644 index 0000000..44d62d2 --- /dev/null +++ b/ramda/toPairsIn.py @@ -0,0 +1,13 @@ +from .keysIn import keysIn +from .private._curry1 import _curry1 +from .private._helper import getAttribute + + +def inner_toPairsIn(obj): + pairs = [] + for key in keysIn(obj): + pairs.append([key, getAttribute(obj, key)]) + return pairs + + +toPairsIn = _curry1(inner_toPairsIn) diff --git a/test/test_toPairsIn.py b/test/test_toPairsIn.py new file mode 100644 index 0000000..798da38 --- /dev/null +++ b/test/test_toPairsIn.py @@ -0,0 +1,43 @@ + +import unittest + +import ramda as R + +""" +https://github.com/ramda/ramda/blob/master/test/toPairsIn.js +""" + + +class TestToPairsIn(unittest.TestCase): + def test_converts_a_dict_into_an_array_of_two_element_key_value_arrays(self): + self.assertEqual([['a', 1], ['b', 2], ['c', 3]], R.toPairsIn({'a': 1, 'b': 2, 'c': 3})) + + def test_converts_an_object_into_an_array_of_two_element_key_value_arrays(self): + class A: + a = 'you can see me' + + def __init__(self, b): + self.b = b + a = A('b') + self.assertEqual([['a', 'you can see me'], ['b', 'b']], R.toPairsIn(a)) + + class B(A): + c = 'you can see me too' + + def __init__(self, b, d): + super().__init__(b) + self.d = d + b = B('b', 'd') + self.assertEqual([['c', 'you can see me too'], ['a', 'you can see me'], ['b', 'b'], ['d', 'd']], R.toPairsIn(b)) + + class C(A): + c = 'you can see me too' + + def __init__(self, d): + self.d = d + c = C('d') + self.assertEqual([['c', 'you can see me too'], ['a', 'you can see me'], ['d', 'd']], R.toPairsIn(c)) + + +if __name__ == '__main__': + unittest.main()