jmespath.py icon indicating copy to clipboard operation
jmespath.py copied to clipboard

Can I use map to do some calculation

Open ChenXiaoTemp opened this issue 8 years ago • 1 comments

User case:

[1,2,3]

What I want :

[2,3,4]

I have tried

map(&@+2,@)

it not works.

ChenXiaoTemp avatar Nov 10 '17 10:11 ChenXiaoTemp

import jmespath
from jmespath import functions

# 1. Create a subclass of functions.Functions.
#    The function.Functions base class has logic
#    that introspects all of its methods and automatically
#    registers your custom functions in its function table.
class CustomFunctions(functions.Functions):
    # Here's another example.  This is creating
    # a jmespath function called "my_add" that expects
    # two arguments, both of which should be of type number.
    @functions.signature({'types': ['number']}, {'types': ['number']})
    def _func_my_add(self, x, y):
        return x + y


# 4. Provide an instance of your subclass in a Options object.
options = jmespath.Options(custom_functions=CustomFunctions())

# this will yield [3,4,5]
print(
    jmespath.search(
        "map(&my_add(@, `2`), @)",
        [1,2,3],
        options=options
    )
)

wjo1212 avatar Jan 24 '18 02:01 wjo1212