@@ -96,6 +96,85 @@ of your dict keys. To do this you can use either of these options:
9696 ... jmespath.Options(dict_cls = collections.OrderedDict))
9797
9898
99+ Custom Functions
100+ ~~~~~~~~~~~~~~~~
101+
102+ The JMESPath language has numerous
103+ `built-in functions
104+ <http://jmespath.org/specification.html#built-in-functions> `__, but it is
105+ also possible to add your own custom functions. Keep in mind that
106+ custom function support in jmespath.py is experimental and the API may
107+ change based on feedback.
108+
109+ **If you have a custom function that you've found useful, consider submitting
110+ it to jmespath.site and propose that it be added to the JMESPath language. **
111+ You can submit proposals
112+ `here <https://github.com/jmespath/jmespath.site/issues >`__.
113+
114+ To create custom functions:
115+
116+ * Create a subclass of ``jmespath.functions.Functions ``.
117+ * Create a method with the name ``_func_<your function name> ``.
118+ * Apply the ``jmespath.functions.signature `` decorator that indicates
119+ the expected types of the function arguments.
120+ * Provide an instance of your subclass in a ``jmespath.Options `` object.
121+
122+ Below are a few examples:
123+
124+ .. code :: python
125+
126+ import jmespath
127+ from jmespath import functions
128+
129+ # 1. Create a subclass of functions.Functions.
130+ # The function.Functions base class has logic
131+ # that introspects all of its methods and automatically
132+ # registers your custom functions in its function table.
133+ class CustomFunctions (functions .Functions ):
134+
135+ # 2 and 3. Create a function that starts with _func_
136+ # and decorate it with @signature which indicates its
137+ # expected types.
138+ # In this example, we're creating a jmespath function
139+ # called "unique_letters" that accepts a single argument
140+ # with an expected type "string".
141+ @functions.signature ({' types' : [' string' ]})
142+ def _func_unique_letters (self , s ):
143+ # Given a string s, return a sorted
144+ # string of unique letters: 'ccbbadd' -> 'abcd'
145+ return ' ' .join(sorted (set (s)))
146+
147+ # Here's another example. This is creating
148+ # a jmespath function called "my_add" that expects
149+ # two arguments, both of which should be of type number.
150+ @functions.signature ({' types' : [' number' ]}, {' types' : [' number' ]})
151+ def _func_my_add (self , x , y ):
152+ return x + y
153+
154+ # 4. Provide an instance of your subclass in a Options object.
155+ options = jmespath.Options(custom_functions = CustomFunctions())
156+
157+ # Provide this value to jmespath.search:
158+ # This will print 3
159+ print (
160+ jmespath.search(
161+ ' my_add(`1`, `2`)' , {}, options = options)
162+ )
163+
164+ # This will print "abcd"
165+ print (
166+ jmespath.search(
167+ ' foo.bar | unique_letters(@)' ,
168+ {' foo' : {' bar' : ' ccbbadd' }},
169+ options = options)
170+ )
171+
172+ Again, if you come up with useful functions that you think make
173+ sense in the JMESPath language (and make sense to implement in all
174+ JMESPath libraries, not just python), please let us know at
175+ `jmespath.site <https://github.com/jmespath/jmespath.site/issues >`__.
176+
177+
99178Specification
100179=============
101180
0 commit comments