【问题标题】:Any shortcut to add '>>>' in front of lines in the docstring for doctest?在 doctest 的文档字符串的行前添加“>>>”的任何快捷方式?
【发布时间】:2020-03-21 22:25:48
【问题描述】:

我们将几行代码复制到docstring中进行doc test后,是否有快捷方式自动在代码前添加'>>>'?

比如下面的docstring,doctest的代码很长,每行手动加'>>>'真的很繁琐

def iter_leaves_with_another(tree: Mapping, iter_with: Mapping, default: Callable = dict):
    """
    Iterates through the leaves of the tree (represented by nested mappings), together with another mapping.
    This method can be applied to construct a dictionary with the same structure as the `tree`, with some transformations on the values.

    For example,
    >>> import utilx.dict_ext as dx
    >>> reconstruct = {}
    >>> for d, k, v in dx.iter_leaves_with_another({
    >>>     'a': 1,
    >>>     'b': {'b1': 2,
    >>>           'b2': {'b21': 3,
    >>>                  'b22': 4,
    >>>                  'b23': {'b231': 5}}},
    >>>     'c': 6,
    >>>     'd': {'d1': 7,
    >>>           'd2': 8,
    >>>           'd3': {},
    >>>           'd4': {'d41': 9,
    >>>                  'd42': 10}}
    >>> }, iter_with=reconstruct, default=dict):
    >>>     d[k] = v + 1
    >>>
    >>> # the following prints out the same tree structure, with all values being added by 1.
    >>> # i.e. {'a': 2, 'b': {'b1': 3, 'b2': {'b21': 4, 'b22': 5, 'b23': {'b231': 6}}}, 'c': 7, 'd': {'d1': 8, 'd2': 9, 'd3': {}, 'd4': {'d41': 10, 'd42': 11}}}
    >>> print(reconstruct)

    :param tree: represented by a nested mapping.
    :param iter_with: iterate through the leaves of the `tree` together with this mapping.
    :param default: a callable that returns an object assigned to a key when it is not found in the `iter_with`.
    :return: an iterator; yields a three-tuple at a time: 1) a sub-mapping in `iter_with` that corresponds to the current level in the `tree` being iterated through; 2) the key; 3) the value.
    """
    for k, v in tree.items():
        if isinstance(v, Mapping):
            if k not in iter_with:
                iter_with[k] = default()
            yield from iter_leaves_with_another(tree=v, iter_with=iter_with[k], default=default)
        else:
            yield iter_with, k, v

【问题讨论】:

    标签: python pycharm docstring doctest


    【解决方案1】:

    如果你想为一个文档字符串构建一个文档测试,你不应该只是将代码复制粘贴到文档字符串中并在前面添加>>>。添加>>> 甚至不是正确的做法。您不包括输出,并且您正在使用 >>> 提示,而 ... 提示应该是。

    Doctests 看起来像一个交互式解释器会话。要构建一个 doctest,实际运行一个交互式解释器会话,并将脚本复制粘贴到 docstring 中。

    【讨论】:

      【解决方案2】:

      这没有任何快捷方式,因为可以使用替换轻松完成。 (转至Edit>Find>Replace,或按Ctrl + R

      1. 启用RegEx,将最右侧的图标(带有.* 的图标)按到搜索字段。
      2. 按右数第二个图标启用In Selection
      3. 搜索字段中输入换行符\n
      4. 替换字段中输入\n>>>
      5. 用鼠标选择你想要的行,然后按全部替换

      填写所有字段后按下全部替换后的屏幕截图。

      屏幕截图按下全部替换。

      如果您需要一定程度的缩进,只需在 \n>>> 之间的替换字段中添加必要的空格。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-03-05
        • 2018-03-30
        • 2012-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多