Python函数定义在控制台中工作,但未导入到i​​python笔记本中

时间:2015-03-30 19:24:40

标签: python ipython ipython-notebook

我正在为一个项目开发一些帮助函数,我倾向于使用ipython-notebook来帮助dev。无论如何我碰到了一个奇怪的事情,我的函数定义在python控制台,ipython控制台上工作,或者如果我将函数定义粘贴到ipython-notebook单元格中。

但是,每当我尝试从ipython-notebook中加载模块并随后运行所述函数时,我都会收到以下错误。此外,如果我从控制台运行该函数并像往常一样导入或在ipython控制台中导入,一切正常。我可以导入该功能,它的工作原理。

要清楚,我实际上可以将该函数导入到ipython-notebook中,但实际运行该函数(导入后)会给出以下错误。在导入和运行相同功能的情况下,尽管代码完全相同,但其他方式(不在ipython-notebook中)不会产生相同的错误。

TypeError                                 Traceback (most recent call last)
<ipython-input-53-f4cd5ccd5841> in <module>()
----> 1 fr(100, 1000, c_unit='micro', i_unit='micro')

TypeError: resonant_frequency() got an unexpected keyword argument 'c_unit'

功能定义

def resonant_frequency(capacitance, inductance, c_unit='base', i_unit='base'):
    ''' *** electronics.electronics.py
        * Useful for LC circuit work
        * Both the capacitance and inductance should be in there
          base measurment units. I.e. 100 uHz (micro-hertz) should 
          be 0.0001 Hz etc.

        * Resonant frequency equation is fr = 1 / (2Pi * squrt(capacitance * inductance))

        **********************************************************************************
        TODO make this function discriminate bewtween units so that a unit can be suffixed
        to the output, irregardless of what the inputs were (strings with unit suffixes in
        this case )
        **********************************************************************************
        Unit Table
        ----------

        |Text|Symbol|Factor|
        --------------------
        tera    T      1 000 000 000 000
        giga    G      1 000 000 000
        mega    M      1 000 000
        kilo    k      1 000
        hecto   h      1 00
        deca    da     10 # prob. will not ever use this but it's here
        (none)  (none) 1
        deci    d      0.1
        centi   c      0.01
        milli   m      0.001
        micro   µ      0.000 001 # the letter 'u' can be used here
        nano    n      0.000 000 001
        pico    p      0.000 000 000 001

        *******************************************

        **kwargs can be 'base' (automatic) or any of the prefix
        letters of above

        '''
    # convert capacitance and inductance from strings
    # to there respective base values
    units = {
        'T'   : 1000000000000,
        'G'   : 1000000000,
        'M'   : 1000000,
        'k'   : 1000,
        'h'   : 100,
        'da'  : 10,
        'base': 1,
        'd'   : .1,
        'c'   : .01,
        'm'   : .001,
        'u'   : .000001,
        'n'   : .000000001,
        'p'   : 0.000000000001
    }

    units1 = {
        'tera'   : 1000000000000,
        'giga'   : 1000000000,
        'mega'   : 1000000,
        'kilo'   : 1000,
        'hecta'  : 100,
        'deca'   : 10,
        'base'   : 1,
        'deci'   : 0.1,
        'centi'  : 0.01,
        'milli'  : 0.001,
        'micro'  : 0.000001,
        'nano'   : 0.000000001,
        'pico'   : 0.000000000001
    }

    # This should return frequency in Hz 
    if c_unit == 'base' and i_unit == 'base':
        return 1/((2*math.pi) * math.sqrt(float(capacitance) * float(inductance)))  

    elif c_unit in units and i_unit in units:
        return  1/((2*math.pi) * math.sqrt((float(capacitance)* units[c_unit])\
                * (float(inductance)*units[i_unit])))

    elif c_unit in units1 and i_unit in units1:
        return 1/((2*math.pi) * math.sqrt((float(capacitance)* units1[c_unit])\
                * (float(inductance)*units1[i_unit])))

0 个答案:

没有答案