如何使用Sphinx在Python文档字符串中指示有效范围?

问题描述

是否有一种方法可以使用Sphinx在Python文档字符串中指示“有效范围”?例如,考虑以下线性函数

def f(m,x,b):
    """
    Returns the `y` value of a linear function using slope-intercept form.
    
    :param x: The x-axis value.
    :type x: float
    :param m: The slope of the linear function.
    :type m: float
    :param b: The y-intercept.
    :type b: float
    """
    if x < 0:
        raise ValueError('The min "x" value of this function is 0')
    return m * x + b

是否有一种方法可以将x的域表示为类似“ x必须大于零”?或使用间隔符号[0,infinity]

具体来说,是否有一种方法可以使用Sphinx在Python文档字符串中对此进行记录?

解决方法

默认情况下,Python modules are UTF-8已编码,因此字符将正常呈现。可以使用Unicode字符或文档字符串中相应的hexadecimal code using the u前缀来编写字符串文字。这使得Unicode range for math可以写入文档字符串中。

Python读取程序文本作为Unicode代码点;源文件的编码可以通过编码声明指定,默认为UTF-8,有关详细信息,请参见PEP 3120。

使用Google样式文档字符串的示例字符串文字(带有显式写的Unicode字符和带有前缀u的字符串):

def f(m,x,b) -> float:
    """
    Returns the `y` value of a linear function using slope-intercept form.

    Args:
        x (float): The x-axis value.
        m (float): The slope of the linear function.
        b (float): The y-intercept.
    Returns:
        float: The y-axis value.
    Raises:
        ValueError: Value of `x` ∈ [0,∞],or `x` \u2208\u005B 0,\u221E\u005D.

    """
    if x < 0:
        raise ValueError('The min "x" value of this function is 0')
    return m * x + b

结果:

enter image description here

如果您想编写更复杂的数学表达式Sphinx has several extensions that allow to output them as HTML,则此方法适用于简单的方程式。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...