2017-04-16 89 views
0

我很喜欢Python的初学者,但在java中,您通常会使用这些导入准确地显示对象来自哪里。例如,import语句下面告诉我SpringBootApplication对象从Spring Boot类可直接接触,我可以潜入类读取所有的方法代码:用于zipline的方法的python代码在哪里?

import org.springframework.boot.autoconfigure.SpringBootApplication; 

现在我看溜索库蟒蛇:

https://github.com/quantopian/zipline

这是从他们的样本代码的GitHub库的主页上:

from zipline.api import (
    history, 
    order_target, 
    record, 
    symbol, 
) 

所以我看看zipline文件夹,看看是否有从中导入方法history, order_target, record, symbol的api文件,因为我想读取驱动这些方法的底层代码。

的代码并没有告诉我很多(https://github.com/quantopian/zipline/blob/master/zipline/api.py):

from .finance.asset_restrictions import (
    Restriction, 
    StaticRestrictions, 
    HistoricalRestrictions, 
    RESTRICTION_STATES, 
) 
from .finance import commission, execution, slippage, cancel_policy 
from .finance.cancel_policy import (
    NeverCancel, 
    EODCancel 
) 
from .finance.slippage import (
    FixedSlippage, 
    VolumeShareSlippage, 
) 
from .utils import math_utils, events 
from .utils.events import (
    date_rules, 
    time_rules 
) 

__all__ = [ 
    'EODCancel', 
    'FixedSlippage', 
    'NeverCancel', 
    'VolumeShareSlippage', 
    'Restriction', 
    'StaticRestrictions', 
    'HistoricalRestrictions', 
    'RESTRICTION_STATES', 
    'cancel_policy', 
    'commission', 
    'date_rules', 
    'events', 
    'execution', 
    'math_utils', 
    'slippage', 
    'time_rules' 
] 

然而,有一个叫做api.pyi的文件,似乎包含了什么我感兴趣的确实的方法的一些文字(https://github.com/quantopian/zipline/blob/master/zipline/api.pyi) 。例如,该方法record,它说:

def record(*args, **kwargs): 
    """Track and record values each day. 

    Parameters 
    ---------- 
    **kwargs 
     The names and values to record. 

    Notes 
    ----- 
    These values will appear in the performance packets and the performance 
    dataframe passed to ``analyze`` and returned from 
    :func:`~zipline.run_algorithm`. 
    """ 

我想也许代码坐在内zipline.run_algorithm,也有一脸的zipline/run_algorithm文件,但在回购无法找到它。

在Python中为这些方法保存的代码在哪里?我只想阅读代码以更好地理解它的工作原理。

+0

我有一点使用Java的经验。在Python中,您可以检查对象并确定它们的位置,例如,如果您导入了一个模块:'import test',那么'test .__ file__'将为您提供模块test所在的目录。包和所有其他对象一样,都有一个相应的内建属性,可以提供某种关于该对象的某些信息,'dir(object)'返回许多细节。 – direprobs

回答

3

zipline正在使用某种复杂而不寻常的导入结构。线索是在api.py此评论:

# Note that part of the API is implemented in TradingAlgorithm as 
# methods (e.g. order). These are added to this namespace via the 
# decorator ``api_method`` inside of algorithm.py. 

如果您在algorithm.py看可以看到这些record,并与@api_method装饰定义这些方法的其余部分。 (如果你看看zipline/utils/api_support.py,你可以看到装饰器本身的代码,它通过使用setattr将这些方法添加到zipline.api中。)