{"id":15797851,"url":"https://github.com/qequ/jmespathcustomfunctions","last_synced_at":"2025-03-31T19:45:28.885Z","repository":{"id":37915026,"uuid":"506349430","full_name":"qequ/jmespathCustomFunctions","owner":"qequ","description":null,"archived":false,"fork":false,"pushed_at":"2022-06-22T18:07:12.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-12T00:42:32.959Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/qequ.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-06-22T17:45:45.000Z","updated_at":"2022-06-22T17:53:43.000Z","dependencies_parsed_at":"2022-08-24T17:01:55.717Z","dependency_job_id":null,"html_url":"https://github.com/qequ/jmespathCustomFunctions","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qequ%2FjmespathCustomFunctions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qequ%2FjmespathCustomFunctions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qequ%2FjmespathCustomFunctions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qequ%2FjmespathCustomFunctions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/qequ","download_url":"https://codeload.github.com/qequ/jmespathCustomFunctions/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246531986,"owners_count":20792735,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-05T00:21:26.187Z","updated_at":"2025-03-31T19:45:28.867Z","avatar_url":"https://github.com/qequ.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"jmespathCustomFunctions\n========\n\n\nCustom Functions\n~~~~~~~~~~~~~~~~\n\n(Reference: `jmespath.py repo \u003chttps://github.com/jmespath/jmespath.py\u003e`__)\nThe JMESPath language has numerous\n`built-in functions\n\u003chttp://jmespath.org/specification.html#built-in-functions\u003e`__, but it is\nalso possible to add your own custom functions.  Keep in mind that\ncustom function support in jmespath.py is experimental and the API may\nchange based on feedback.\n\n**If you have a custom function that you've found useful, consider submitting\nit to jmespath.site and propose that it be added to the JMESPath language.**\nYou can submit proposals\n`here \u003chttps://github.com/jmespath/jmespath.site/issues\u003e`__.\n\nTo create custom functions:\n\n* Create a subclass of ``jmespath.functions.Functions``.\n* Create a method with the name ``_func_\u003cyour function name\u003e``.\n* Apply the ``jmespath.functions.signature`` decorator that indicates\n  the expected types of the function arguments.\n* Provide an instance of your subclass in a ``jmespath.Options`` object.\n\nBelow are a few examples:\n\n.. code:: python\n\n    import jmespath\n    from jmespath import functions\n\n    # 1. Create a subclass of functions.Functions.\n    #    The function.Functions base class has logic\n    #    that introspects all of its methods and automatically\n    #    registers your custom functions in its function table.\n    class CustomFunctions(functions.Functions):\n\n        # 2 and 3.  Create a function that starts with _func_\n        # and decorate it with @signature which indicates its\n        # expected types.\n        # In this example, we're creating a jmespath function\n        # called \"unique_letters\" that accepts a single argument\n        # with an expected type \"string\".\n        @functions.signature({'types': ['string']})\n        def _func_unique_letters(self, s):\n            # Given a string s, return a sorted\n            # string of unique letters: 'ccbbadd' -\u003e  'abcd'\n            return ''.join(sorted(set(s)))\n\n        # Here's another example.  This is creating\n        # a jmespath function called \"my_add\" that expects\n        # two arguments, both of which should be of type number.\n        @functions.signature({'types': ['number']}, {'types': ['number']})\n        def _func_my_add(self, x, y):\n            return x + y\n\n    # 4. Provide an instance of your subclass in a Options object.\n    options = jmespath.Options(custom_functions=CustomFunctions())\n\n    # Provide this value to jmespath.search:\n    # This will print 3\n    print(\n        jmespath.search(\n            'my_add(`1`, `2`)', {}, options=options)\n    )\n\n    # This will print \"abcd\"\n    print(\n        jmespath.search(\n            'foo.bar | unique_letters(@)',\n            {'foo': {'bar': 'ccbbadd'}},\n            options=options)\n    )\n\n\n\n\nAnother example\n\n\n.. code:: python\n\n    import jmespath\n    from jmespath import functions\n\n    class RemoveFunctions(functions.Functions):\n\n        @functions.signature({'types': ['object'], \"variadic\": True}, {'types': ['boolean']})\n        def _func_remove_null(self, obj, recursive):\n            if recursive:\n                return self._func_remove_recursive(obj, None)\n            else:\n                return self._func_remove(obj, None)\n\n        @functions.signature({'types': ['object'], \"variadic\": True}, {'types': ['boolean']})\n        def _func_remove_empty(self, obj, recursive):\n            if recursive:\n                return self._func_remove_recursive(obj, None, '')\n            else:\n                return self._func_remove(obj, None, '')\n\n        def _func_remove(self, obj, *args):\n            return {k: v for k, v in obj.items() if not v in list(args)}\n\n        def _func_remove_recursive(self, obj, *args):\n            ret_dict = {}\n            for k in obj:\n                if not obj[k] in list(args):\n                    ret_dict[k] = obj[k]\n\n                if isinstance(obj[k], dict):\n                    ret_dict[k] = self._func_remove_recursive(obj[k], *args)\n\n            return ret_dict\n\n\n    options = jmespath.Options(custom_functions=RemoveFunctions())\n\n    # print: {'a': 'a', 'd': {'b': 'b'}}\n    print(\n        jmespath.search(\n            '{a: `a`, c: null, d: {a: null, b: `b`}} | remove_empty(@, `true`)', {}, options=options)\n    )\n\n\n    # print {'a': 'a', 'd': {'b': 'b'}}\n    print(\n        jmespath.search(\n            '{a: `a`, c: null, d: {a: null, b: `b`, c:``}} | remove_empty(@, `true`)', {}, options=options)\n    )\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqequ%2Fjmespathcustomfunctions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqequ%2Fjmespathcustomfunctions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqequ%2Fjmespathcustomfunctions/lists"}