GithubHelp home page GithubHelp logo

Custom functions about jmespath.py HOT 7 CLOSED

jmespath avatar jmespath commented on June 19, 2024
Custom functions

from jmespath.py.

Comments (7)

jamesls avatar jamesls commented on June 19, 2024

I agree that I would like to have a way to register custom functions in a simple way. Some of the other language implementation of JMESPath support this so I'd like to do so with python. Let me see what I can some up with.

Additionally, I'm thinking that it might actually be worth just having some sort of conditional expression in the JMESPath language. I think that's a common enough request. Either via if/else statements, or possibly with something like C's ternary operator.

from jmespath.py.

meric avatar meric commented on June 19, 2024

Is there anything I can do to assist in pursuing these objectives? I'd love to help if I can.

from jmespath.py.

arthurprs avatar arthurprs commented on June 19, 2024

I'd love to have a way to evaluate boolean expressions, this is the type of hack I'm resorting to [geo.country][?@=='UK']

from jmespath.py.

meric avatar meric commented on June 19, 2024

Could we perhaps implement it in Options? Feed through functions in a dictionary or class with static attributes there.

from jmespath.py.

jamesls avatar jamesls commented on June 19, 2024

I've pushed a branch that adds support for adding custom functions via Options as suggested, but the API is a little too low level for me to merge. The RuntimeFunctions class was not designed as a public facing API so I need to restructure some of the internals first.

That API I'm shooting for is you can just subclass from a base class and anything with a function signature (and likely a naming convention) will be registered as a jmespath function. Let me play around with a few ideas.

from jmespath.py.

jamesls avatar jamesls commented on June 19, 2024

I ended up finding an API that seemed reasonable to me, and still allowed you to leverage the type checking/arity checking that built in jmespath functions have.

Let me know if you have any feedback: #102

from jmespath.py.

rageycomma avatar rageycomma commented on June 19, 2024

Just in case this is useful for anyone, I found a way to do it (sorry for posting on a dead issue but it's #1 result on google..)

Background: I have a field which is titled "Facebook" but sometimes contains VK URLs due to an input error. I wanted to do something like:

if contains vk.com
name = VKontakte 
else 
name = Facebook

To accomplish that, I'm making a custom group from data, where Facebook is a property on an object like this:

{
"Facebook": "http://vk.com/blahblah"
}

I want to create an object like this when it contains Facebook:

{
"name": "VKontakte",
"value": "http://vk.com/blahblah"
}

And like this when it contains Facebook:

{
"name": "Facebook",
"value": "http://www.facebook.com/blahblah"
}

And the way I accomplished that was:

thisProp: [
 { "name": 'VKontakte', "display": contains(Facebook,'vk.com'), "value": Facebook },
 { "name": 'Facebook', "display": contains(Facebook, 'facebook.com'), "value": Facebook},
][?display]

Then finally remove the field you just created to check it:

thisProp: [
 { "name": 'VKontakte', "display": contains(Facebook,'vk.com'), "value": Facebook },
 { "name": 'Facebook', "display": contains(Facebook, 'facebook.com'), "value": Facebook},
][?display].{ "name": name, "value": value }

from jmespath.py.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.