简体   繁体   English

如何从reStructuredText获取属性的类型?

[英]How get Type of attribute from reStructuredText?

How get Type of attribute from reStructuredText? 如何从reStructuredText获取属性的类型? I have something like this. 我有这样的东西。

@property
def port(self):
    """Get port.

    :return: port
    :rtype: str
    """
    return self._port

How can i get type of attribute (str)? 如何获取属性类型(str)?

Is the code fragment yours? 代码片段是您的吗? If yes, you can use whatever mechanics you like, eg add an attribute directly to the function for later inspection: SomeClass.port.fget.return_type = int . 如果是,则可以使用任何喜欢的机制,例如,直接向函数添加属性以供以后检查: SomeClass.port.fget.return_type = int Try it, it works. 试试吧,它有效。 Only be careful to qualify the property via the class, not via the instance (or use .__class__ on an instance). 仅注意通过类而不是通过实例限定属性(或在实例上使用.__class__ )。

If not, parse the port.__doc__ and extract the relevant field. 如果不是,请解析 port.__doc__并提取相关字段。

If you are CPU-constrained, you can still use eg SQLAlchemy without the ORM layer. 如果您受CPU限制,则仍可以使用不带ORM层的SQLAlchemy。 It can produce very efficient SQL from table models ahead of time, eg during startup. 它可以提前(例如在启动过程中)从表模型中生成非常高效的SQL。 Then you just invoke them with right parameters, paying rather little CPU time. 然后,您只需使用正确的参数调用它们,而只需花费很少的CPU时间。 In most cases, the DB round-trip dwarfs the query creation and results decoding anyway. 在大多数情况下,数据库往返总是使查询创建和结果解码相形见war。

Same applies to parsing the RST: you can do it ahead of time, eg as a build step, and store the information in an easy-to-parse file, eg JSON or even as pickled Python data, and quickly load it as startup. 解析RST同样适用:您可以提前进行构建(例如,作为构建步骤),并将信息存储在易于解析的文件(例如JSON或腌制的Python数据)中,并在启动时快速加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM