m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

379 lines
18 KiB

6 months ago
  1. Metadata-Version: 2.1
  2. Name: jsonpath-ng
  3. Version: 1.6.1
  4. Summary: A final implementation of JSONPath for Python that aims to be standard compliant, including arithmetic and binary comparison operators and providing clear AST for metaprogramming.
  5. Home-page: https://github.com/h2non/jsonpath-ng
  6. Author: Tomas Aparicio
  7. Author-email: tomas@aparicio.me
  8. License: Apache 2.0
  9. Classifier: Development Status :: 5 - Production/Stable
  10. Classifier: Intended Audience :: Developers
  11. Classifier: License :: OSI Approved :: Apache Software License
  12. Classifier: Programming Language :: Python :: 3
  13. Classifier: Programming Language :: Python :: 3.7
  14. Classifier: Programming Language :: Python :: 3.8
  15. Classifier: Programming Language :: Python :: 3.9
  16. Classifier: Programming Language :: Python :: 3.10
  17. Classifier: Programming Language :: Python :: 3.11
  18. Classifier: Programming Language :: Python :: 3.12
  19. License-File: LICENSE
  20. Requires-Dist: ply
  21. Python JSONPath Next-Generation |Build Status| |PyPI|
  22. =====================================================
  23. A final implementation of JSONPath for Python that aims to be standard compliant, including arithmetic
  24. and binary comparison operators, as defined in the original `JSONPath proposal`_.
  25. This packages merges both `jsonpath-rw`_ and `jsonpath-rw-ext`_ and
  26. provides several AST API enhancements, such as the ability to update or remove nodes in the tree.
  27. About
  28. -----
  29. This library provides a robust and significantly extended implementation
  30. of JSONPath for Python. It is tested with CPython 3.7 and higher.
  31. This library differs from other JSONPath implementations in that it is a
  32. full *language* implementation, meaning the JSONPath expressions are
  33. first class objects, easy to analyze, transform, parse, print, and
  34. extend.
  35. Quick Start
  36. -----------
  37. To install, use pip:
  38. .. code:: bash
  39. $ pip install --upgrade jsonpath-ng
  40. Usage
  41. -----
  42. Basic examples:
  43. .. code:: python
  44. $ python
  45. >>> from jsonpath_ng import jsonpath, parse
  46. # A robust parser, not just a regex. (Makes powerful extensions possible; see below)
  47. >>> jsonpath_expr = parse('foo[*].baz')
  48. # Extracting values is easy
  49. >>> [match.value for match in jsonpath_expr.find({'foo': [{'baz': 1}, {'baz': 2}]})]
  50. [1, 2]
  51. # Matches remember where they came from
  52. >>> [str(match.full_path) for match in jsonpath_expr.find({'foo': [{'baz': 1}, {'baz': 2}]})]
  53. ['foo.[0].baz', 'foo.[1].baz']
  54. # Modifying values matching the path
  55. >>> jsonpath_expr.update( {'foo': [{'baz': 1}, {'baz': 2}]}, 3)
  56. {'foo': [{'baz': 3}, {'baz': 3}]}
  57. # Modifying one of the values matching the path
  58. >>> matches = jsonpath_expr.find({'foo': [{'baz': 1}, {'baz': 2}]})
  59. >>> matches[0].full_path.update( {'foo': [{'baz': 1}, {'baz': 2}]}, 3)
  60. {'foo': [{'baz': 3}, {'baz': 2}]}
  61. # Removing all values matching a path
  62. >>> jsonpath_expr.filter(lambda d: True, {'foo': [{'baz': 1}, {'baz': 2}]})
  63. {'foo': [{}, {}]}
  64. # Removing values containing particular data matching path
  65. >>> jsonpath_expr.filter(lambda d: d == 2, {'foo': [{'baz': 1}, {'baz': 2}]})
  66. {'foo': [{'baz': 1}, {}]}
  67. # And this can be useful for automatically providing ids for bits of data that do not have them (currently a global switch)
  68. >>> jsonpath.auto_id_field = 'id'
  69. >>> [match.value for match in parse('foo[*].id').find({'foo': [{'id': 'bizzle'}, {'baz': 3}]})]
  70. ['foo.bizzle', 'foo.[1]']
  71. # A handy extension: named operators like `parent`
  72. >>> [match.value for match in parse('a.*.b.`parent`.c').find({'a': {'x': {'b': 1, 'c': 'number one'}, 'y': {'b': 2, 'c': 'number two'}}})]
  73. ['number two', 'number one']
  74. # You can also build expressions directly quite easily
  75. >>> from jsonpath_ng.jsonpath import Fields
  76. >>> from jsonpath_ng.jsonpath import Slice
  77. >>> jsonpath_expr_direct = Fields('foo').child(Slice('*')).child(Fields('baz')) # This is equivalent
  78. Using the extended parser:
  79. .. code:: python
  80. $ python
  81. >>> from jsonpath_ng.ext import parse
  82. # A robust parser, not just a regex. (Makes powerful extensions possible; see below)
  83. >>> jsonpath_expr = parse('foo[*].baz')
  84. JSONPath Syntax
  85. ---------------
  86. The JSONPath syntax supported by this library includes some additional
  87. features and omits some problematic features (those that make it
  88. unportable). In particular, some new operators such as ``|`` and
  89. ``where`` are available, and parentheses are used for grouping not for
  90. callbacks into Python, since with these changes the language is not
  91. trivially associative. Also, fields may be quoted whether or not they
  92. are contained in brackets.
  93. Atomic expressions:
  94. +-----------------------+---------------------------------------------------------------------------------------------+
  95. | Syntax | Meaning |
  96. +=======================+=============================================================================================+
  97. | ``$`` | The root object |
  98. +-----------------------+---------------------------------------------------------------------------------------------+
  99. | ```this``` | The "current" object. |
  100. +-----------------------+---------------------------------------------------------------------------------------------+
  101. | ```foo``` | More generally, this syntax allows "named operators" to extend JSONPath is arbitrary ways |
  102. +-----------------------+---------------------------------------------------------------------------------------------+
  103. | *field* | Specified field(s), described below |
  104. +-----------------------+---------------------------------------------------------------------------------------------+
  105. | ``[`` *field* ``]`` | Same as *field* |
  106. +-----------------------+---------------------------------------------------------------------------------------------+
  107. | ``[`` *idx* ``]`` | Array access, described below (this is always unambiguous with field access) |
  108. +-----------------------+---------------------------------------------------------------------------------------------+
  109. Jsonpath operators:
  110. +-------------------------------------+------------------------------------------------------------------------------------+
  111. | Syntax | Meaning |
  112. +=====================================+====================================================================================+
  113. | *jsonpath1* ``.`` *jsonpath2* | All nodes matched by *jsonpath2* starting at any node matching *jsonpath1* |
  114. +-------------------------------------+------------------------------------------------------------------------------------+
  115. | *jsonpath* ``[`` *whatever* ``]`` | Same as *jsonpath*\ ``.``\ *whatever* |
  116. +-------------------------------------+------------------------------------------------------------------------------------+
  117. | *jsonpath1* ``..`` *jsonpath2* | All nodes matched by *jsonpath2* that descend from any node matching *jsonpath1* |
  118. +-------------------------------------+------------------------------------------------------------------------------------+
  119. | *jsonpath1* ``where`` *jsonpath2* | Any nodes matching *jsonpath1* with a child matching *jsonpath2* |
  120. +-------------------------------------+------------------------------------------------------------------------------------+
  121. | *jsonpath1* ``|`` *jsonpath2* | Any nodes matching the union of *jsonpath1* and *jsonpath2* |
  122. +-------------------------------------+------------------------------------------------------------------------------------+
  123. Field specifiers ( *field* ):
  124. +-------------------------+-------------------------------------------------------------------------------------+
  125. | Syntax | Meaning |
  126. +=========================+=====================================================================================+
  127. | ``fieldname`` | the field ``fieldname`` (from the "current" object) |
  128. +-------------------------+-------------------------------------------------------------------------------------+
  129. | ``"fieldname"`` | same as above, for allowing special characters in the fieldname |
  130. +-------------------------+-------------------------------------------------------------------------------------+
  131. | ``'fieldname'`` | ditto |
  132. +-------------------------+-------------------------------------------------------------------------------------+
  133. | ``*`` | any field |
  134. +-------------------------+-------------------------------------------------------------------------------------+
  135. | *field* ``,`` *field* | either of the named fields (you can always build equivalent jsonpath using ``|``) |
  136. +-------------------------+-------------------------------------------------------------------------------------+
  137. Array specifiers ( *idx* ):
  138. +-----------------------------------------+---------------------------------------------------------------------------------------+
  139. | Syntax | Meaning |
  140. +=========================================+=======================================================================================+
  141. | ``[``\ *n*\ ``]`` | array index (may be comma-separated list) |
  142. +-----------------------------------------+---------------------------------------------------------------------------------------+
  143. | ``[``\ *start*\ ``?:``\ *end*\ ``?]`` | array slicing (note that *step* is unimplemented only due to lack of need thus far) |
  144. +-----------------------------------------+---------------------------------------------------------------------------------------+
  145. | ``[*]`` | any array index |
  146. +-----------------------------------------+---------------------------------------------------------------------------------------+
  147. Programmatic JSONPath
  148. ---------------------
  149. If you are programming in Python and would like a more robust way to
  150. create JSONPath expressions that does not depend on a parser, it is very
  151. easy to do so directly, and here are some examples:
  152. - ``Root()``
  153. - ``Slice(start=0, end=None, step=None)``
  154. - ``Fields('foo', 'bar')``
  155. - ``Index(42)``
  156. - ``Child(Fields('foo'), Index(42))``
  157. - ``Where(Slice(), Fields('subfield'))``
  158. - ``Descendants(jsonpath, jsonpath)``
  159. Extras
  160. ------
  161. - *Path data*: The result of ``JsonPath.find`` provide detailed context
  162. and path data so it is easy to traverse to parent objects, print full
  163. paths to pieces of data, and generate automatic ids.
  164. - *Automatic Ids*: If you set ``jsonpath_ng.auto_id_field`` to a value
  165. other than None, then for any piece of data missing that field, it
  166. will be replaced by the JSONPath to it, giving automatic unique ids
  167. to any piece of data. These ids will take into account any ids
  168. already present as well.
  169. - *Named operators*: Instead of using ``@`` to reference the current
  170. object, this library uses ```this```. In general, any string
  171. contained in backquotes can be made to be a new operator, currently
  172. by extending the library.
  173. Extensions
  174. ----------
  175. To use the extensions below you must import from `jsonpath_ng.ext`.
  176. +--------------+-----------------------------------------------+
  177. | name | Example |
  178. +==============+===============================================+
  179. | len | - ``$.objects.`len``` |
  180. +--------------+-----------------------------------------------+
  181. | sub | - ``$.field.`sub(/foo\\\\+(.*)/, \\\\1)``` |
  182. | | - ``$.field.`sub(/regex/, replacement)``` |
  183. +--------------+-----------------------------------------------+
  184. | split | - ``$.field.`split(+, 2, -1)``` |
  185. | | - ``$.field.`split(sep, segement, maxsplit)```|
  186. +--------------+-----------------------------------------------+
  187. | sorted | - ``$.objects.`sorted``` |
  188. | | - ``$.objects[\\some_field]`` |
  189. | | - ``$.objects[\\some_field,/other_field]`` |
  190. +--------------+-----------------------------------------------+
  191. | filter | - ``$.objects[?(@some_field > 5)]`` |
  192. | | - ``$.objects[?some_field = "foobar"]`` |
  193. | | - ``$.objects[?some_field =~ "foobar"]`` |
  194. | | - ``$.objects[?some_field > 5 & other < 2]`` |
  195. | | |
  196. | | Supported operators: |
  197. | | - Equality: ==, =, != |
  198. | | - Comparison: >, >=, <, <= |
  199. | | - Regex match: =~ |
  200. | | |
  201. | | Combine multiple criteria with '&'. |
  202. | | |
  203. | | Properties can only be compared to static |
  204. | | values. |
  205. +--------------+-----------------------------------------------+
  206. | arithmetic | - ``$.foo + "_" + $.bar`` |
  207. | (-+*/) | - ``$.foo * 12`` |
  208. | | - ``$.objects[*].cow + $.objects[*].cat`` |
  209. +--------------+-----------------------------------------------+
  210. About arithmetic and string
  211. ---------------------------
  212. Operations are done with python operators and allows types that python
  213. allows, and return [] if the operation can be done due to incompatible types.
  214. When operators are used, a jsonpath must be be fully defined otherwise
  215. jsonpath-rw-ext can't known if the expression is a string or a jsonpath field,
  216. in this case it will choice string as type.
  217. Example with data::
  218. {
  219. 'cow': 'foo',
  220. 'fish': 'bar'
  221. }
  222. | ``cow + fish`` returns ``cowfish``
  223. | ``$.cow + $.fish`` returns ``foobar``
  224. | ``$.cow + "_" + $.fish`` returns ``foo_bar``
  225. | ``$.cow + "_" + fish`` returns ``foo_fish``
  226. About arithmetic and list
  227. -------------------------
  228. Arithmetic can be used against two lists if they have the same size.
  229. Example with data::
  230. {'objects': [
  231. {'cow': 2, 'cat': 3},
  232. {'cow': 4, 'cat': 6}
  233. ]}
  234. | ``$.objects[\*].cow + $.objects[\*].cat`` returns ``[6, 9]``
  235. More to explore
  236. ---------------
  237. There are way too many JSONPath implementations out there to discuss.
  238. Some are robust, some are toy projects that still work fine, some are
  239. exercises. There will undoubtedly be many more. This one is made for use
  240. in released, maintained code, and in particular for programmatic access
  241. to the abstract syntax and extension. But JSONPath at its simplest just
  242. isn't that complicated, so you can probably use any of them
  243. successfully. Why not this one?
  244. The original proposal, as far as I know:
  245. - `JSONPath - XPath for
  246. JSON <http://goessner.net/articles/JSONPath/>`__ by Stefan Goessner.
  247. Other examples
  248. --------------
  249. Loading json data from file
  250. .. code:: python
  251. import json
  252. d = json.loads('{"foo": [{"baz": 1}, {"baz": 2}]}')
  253. # or
  254. with open('myfile.json') as f:
  255. d = json.load(f)
  256. Special note about PLY and docstrings
  257. -------------------------------------
  258. The main parsing toolkit underlying this library,
  259. `PLY <https://github.com/dabeaz/ply>`__, does not work with docstrings
  260. removed. For example, ``PYTHONOPTIMIZE=2`` and ``python -OO`` will both
  261. cause a failure.
  262. Contributors
  263. ------------
  264. This package is authored and maintained by:
  265. - `Kenn Knowles <https://github.com/kennknowles>`__
  266. (`@kennknowles <https://twitter.com/KennKnowles>`__)
  267. - `Tomas Aparicio <https://github.com/h2non>`
  268. with the help of patches submitted by `these contributors <https://github.com/kennknowles/python-jsonpath-ng/graphs/contributors>`__.
  269. Copyright and License
  270. ---------------------
  271. Copyright 2013 - Kenneth Knowles
  272. Copyright 2017 - Tomas Aparicio
  273. Licensed under the Apache License, Version 2.0 (the "License"); you may
  274. not use this file except in compliance with the License. You may obtain
  275. a copy of the License at
  276. ::
  277. http://www.apache.org/licenses/LICENSE-2.0
  278. Unless required by applicable law or agreed to in writing, software
  279. distributed under the License is distributed on an "AS IS" BASIS,
  280. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  281. See the License for the specific language governing permissions and
  282. limitations under the License.
  283. .. _`JSONPath proposal`: http://goessner.net/articles/JsonPath/
  284. .. _`jsonpath-rw`: https://github.com/kennknowles/python-jsonpath-rw
  285. .. _`jsonpath-rw-ext`: https://pypi.python.org/pypi/jsonpath-rw-ext/
  286. .. |PyPi downloads| image:: https://pypip.in/d/jsonpath-ng/badge.png
  287. :target: https://pypi.python.org/pypi/jsonpath-ng
  288. .. |Build Status| image:: https://github.com/h2non/jsonpath-ng/actions/workflows/ci.yml/badge.svg
  289. :target: https://github.com/h2non/jsonpath-ng/actions/workflows/ci.yml
  290. .. |PyPI| image:: https://img.shields.io/pypi/v/jsonpath-ng.svg?maxAge=2592000?style=flat-square
  291. :target: https://pypi.python.org/pypi/jsonpath-ng