48
48
49
49
The ``jmespath.py `` library has two functions
50
50
that operate on python data structures. You can use ``search ``
51
- and give it the jmespath expression and the data::
51
+ and give it the jmespath expression and the data:
52
+
53
+ .. code :: python
52
54
53
55
>> > import jmespath
54
56
>> > path = jmespath.search(' foo.bar' , {' foo' : {' bar' : ' baz' }})
55
57
' baz'
56
58
57
59
Similar to the ``re `` module, you can use the ``compile `` function
58
60
to compile the JMESPath expression and use this parsed expression
59
- to perform repeated searches::
61
+ to perform repeated searches:
62
+
63
+ .. code :: python
60
64
61
65
>> > import jmespath
62
66
>> > expression = jmespath.compile(' foo.bar' )
@@ -75,7 +79,9 @@ Options
75
79
You can provide an instance of ``jmespath.Options `` to control how
76
80
a JMESPath expression is evaluated. The most common scenario for
77
81
using an ``Options `` instance is if you want to have ordered output
78
- of your dict keys. To do this you can use either of these options::
82
+ of your dict keys. To do this you can use either of these options:
83
+
84
+ .. code :: python
79
85
80
86
>> > import jmespath
81
87
>> > jmespath.search(' {a: a, b: b} ,
@@ -90,6 +96,85 @@ of your dict keys. To do this you can use either of these options::
90
96
... jmespath.Options(dict_cls = collections.OrderedDict))
91
97
92
98
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
+
93
178
Specification
94
179
=============
95
180
0 commit comments