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.

143 lines
3.7 KiB

6 months ago
  1. """A module for creating docstrings for sphinx ``data`` domains."""
  2. import re
  3. import textwrap
  4. from ._generic_alias import NDArray
  5. _docstrings_list = []
  6. def add_newdoc(name: str, value: str, doc: str) -> None:
  7. """Append ``_docstrings_list`` with a docstring for `name`.
  8. Parameters
  9. ----------
  10. name : str
  11. The name of the object.
  12. value : str
  13. A string-representation of the object.
  14. doc : str
  15. The docstring of the object.
  16. """
  17. _docstrings_list.append((name, value, doc))
  18. def _parse_docstrings() -> str:
  19. """Convert all docstrings in ``_docstrings_list`` into a single
  20. sphinx-legible text block.
  21. """
  22. type_list_ret = []
  23. for name, value, doc in _docstrings_list:
  24. s = textwrap.dedent(doc).replace("\n", "\n ")
  25. # Replace sections by rubrics
  26. lines = s.split("\n")
  27. new_lines = []
  28. indent = ""
  29. for line in lines:
  30. m = re.match(r'^(\s+)[-=]+\s*$', line)
  31. if m and new_lines:
  32. prev = textwrap.dedent(new_lines.pop())
  33. if prev == "Examples":
  34. indent = ""
  35. new_lines.append(f'{m.group(1)}.. rubric:: {prev}')
  36. else:
  37. indent = 4 * " "
  38. new_lines.append(f'{m.group(1)}.. admonition:: {prev}')
  39. new_lines.append("")
  40. else:
  41. new_lines.append(f"{indent}{line}")
  42. s = "\n".join(new_lines)
  43. # Done.
  44. type_list_ret.append(f""".. data:: {name}\n :value: {value}\n {s}""")
  45. return "\n".join(type_list_ret)
  46. add_newdoc('ArrayLike', 'typing.Union[...]',
  47. """
  48. A `~typing.Union` representing objects that can be coerced into an `~numpy.ndarray`.
  49. Among others this includes the likes of:
  50. * Scalars.
  51. * (Nested) sequences.
  52. * Objects implementing the `~class.__array__` protocol.
  53. See Also
  54. --------
  55. :term:`array_like`:
  56. Any scalar or sequence that can be interpreted as an ndarray.
  57. Examples
  58. --------
  59. .. code-block:: python
  60. >>> import numpy as np
  61. >>> import numpy.typing as npt
  62. >>> def as_array(a: npt.ArrayLike) -> np.ndarray:
  63. ... return np.array(a)
  64. """)
  65. add_newdoc('DTypeLike', 'typing.Union[...]',
  66. """
  67. A `~typing.Union` representing objects that can be coerced into a `~numpy.dtype`.
  68. Among others this includes the likes of:
  69. * :class:`type` objects.
  70. * Character codes or the names of :class:`type` objects.
  71. * Objects with the ``.dtype`` attribute.
  72. See Also
  73. --------
  74. :ref:`Specifying and constructing data types <arrays.dtypes.constructing>`
  75. A comprehensive overview of all objects that can be coerced into data types.
  76. Examples
  77. --------
  78. .. code-block:: python
  79. >>> import numpy as np
  80. >>> import numpy.typing as npt
  81. >>> def as_dtype(d: npt.DTypeLike) -> np.dtype:
  82. ... return np.dtype(d)
  83. """)
  84. add_newdoc('NDArray', repr(NDArray),
  85. """
  86. A :term:`generic <generic type>` version of
  87. `np.ndarray[Any, np.dtype[+ScalarType]] <numpy.ndarray>`.
  88. Can be used during runtime for typing arrays with a given dtype
  89. and unspecified shape.
  90. Examples
  91. --------
  92. .. code-block:: python
  93. >>> import numpy as np
  94. >>> import numpy.typing as npt
  95. >>> print(npt.NDArray)
  96. numpy.ndarray[typing.Any, numpy.dtype[+ScalarType]]
  97. >>> print(npt.NDArray[np.float64])
  98. numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]
  99. >>> NDArrayInt = npt.NDArray[np.int_]
  100. >>> a: NDArrayInt = np.arange(10)
  101. >>> def func(a: npt.ArrayLike) -> npt.NDArray[Any]:
  102. ... return np.array(a)
  103. """)
  104. _docstrings = _parse_docstrings()