2013-06-06 92 views
1

是否可以調用另一個python腳本來訪問腳本中的定義,而不是其他的東西?Python:調用另一個python腳本

在我想導入的腳本中,有一些情節我想壓制,因爲這個程序不需要。也就是說,我只想訪問Stumpff函數的定義而不繪製數字。

我想要導入的腳本是:

#!/usr/bin/env ipython 
# This program plots the Stumpff functions C(z) and S(z) 

import numpy as np 
import pylab 
from matplotlib.ticker import MaxNLocator 


def C(z): 
    if z > 0: 
     return (1 - np.cos(z ** 0.5))/z 
    elif z < 0: 
     return (np.cosh(np.sqrt(-z)) - 1)/-z 
    return 0.5 


def S(z): 
    if z > 0: 
     return (np.sqrt(z) - np.sin(z ** 0.5))/np.sqrt(z) ** 3 
    elif z < 0: 
     return (np.sinh(np.sqrt(-z)) - np.sqrt(-z))/np.sqrt(-z) ** 3 
    return 1.0/6.0 


vC = np.vectorize(C) 
vS = np.vectorize(S) 

z = np.linspace(-50.0, 500.0, 100000.0) 
y = vC(z) 
y2 = vS(z) 

fig = pylab.figure() 
ax = fig.add_subplot(111) 
ax.plot(z, y, 'r') 
ax.plot(z, y2, 'b') 
pylab.legend(('$C(z)$', '$S(z)$'), loc = 0) 
pylab.xlim((-50, 0)) 
pylab.ylim((0, 12)) 
pylab.xlabel('$z$') 
pylab.gca().xaxis.set_major_locator(MaxNLocator(prune = 'lower')) 
pylab.savefig('stumpffneg50to0.eps', format = 'eps') 


fig2 = pylab.figure() 
ax2 = fig2.add_subplot(111) 
ax2.plot(z, y, 'r') 
ax2.plot(z, y2, 'b') 
pylab.legend(('$C(z)$', '$S(z)$'), loc = 1) 
pylab.xlim((0, 30)) 
pylab.ylim((0, 0.5)) 
pylab.xlabel('$z$') 
pylab.gca().xaxis.set_major_locator(MaxNLocator(prune = 'lower')) 
pylab.savefig('stumpff0to30.eps', format = 'eps') 


fig3 = pylab.figure() 
ax3 = fig3.add_subplot(111) 
ax3.plot(z, y, 'r') 
ax3.plot(z, y2, 'b') 
pylab.legend(('$C(z)$', '$S(z)$'), loc = 0) 
pylab.xlim((0, 500)) 
pylab.ylim((0, 0.05)) 
pylab.xlabel('$z$') 
pylab.gca().xaxis.set_major_locator(MaxNLocator(prune = 'lower')) 
pylab.savefig('stumpff0to500.eps', format = 'eps') 
pylab.show() 

從閱讀python how do I call external python programs,我看到,我有加

import stumpff 

在那之後,我的新的腳本瞭解C(z)S(z)

回答

2

您的腳本編寫的方式,沒有辦法導入它,並沒有製作的情節。

爲了使import stumpff能夠正常工作,並且您的腳本將會理解C(z)和S(z),您需要製作繪圖代碼,使其僅在作爲腳本運行時才能運行。要做到這一點的方法之一是把所有的它在main()函數,然後用

if __name__ == '__main__': 
    main() 

或者,只是有這一切該條件下,這樣的:

#!/usr/bin/env ipython 
# This program plots the Stumpff functions C(z) and S(z) 

import numpy as np 
import pylab 
from matplotlib.ticker import MaxNLocator 


def C(z): 
    if z > 0: 
     return (1 - np.cos(z ** 0.5))/z 
    elif z < 0: 
     return (np.cosh(np.sqrt(-z)) - 1)/-z 
    return 0.5 


def S(z): 
    if z > 0: 
     return (np.sqrt(z) - np.sin(z ** 0.5))/np.sqrt(z) ** 3 
    elif z < 0: 
     return (np.sinh(np.sqrt(-z)) - np.sqrt(-z))/np.sqrt(-z) ** 3 
    return 1.0/6.0 


if __name__ == '__main__': 
    vC = np.vectorize(C) 
    vS = np.vectorize(S) 

    z = np.linspace(-50.0, 500.0, 100000.0) 
    y = vC(z) 
    y2 = vS(z) 

    fig = pylab.figure() 
    ax = fig.add_subplot(111) 
    ax.plot(z, y, 'r') 
    ax.plot(z, y2, 'b') 
    pylab.legend(('$C(z)$', '$S(z)$'), loc = 0) 
    pylab.xlim((-50, 0)) 
    pylab.ylim((0, 12)) 
    pylab.xlabel('$z$') 
    pylab.gca().xaxis.set_major_locator(MaxNLocator(prune = 'lower')) 
    pylab.savefig('stumpffneg50to0.eps', format = 'eps') 


    fig2 = pylab.figure() 
    ax2 = fig2.add_subplot(111) 
    ax2.plot(z, y, 'r') 
    ax2.plot(z, y2, 'b') 
    pylab.legend(('$C(z)$', '$S(z)$'), loc = 1) 
    pylab.xlim((0, 30)) 
    pylab.ylim((0, 0.5)) 
    pylab.xlabel('$z$') 
    pylab.gca().xaxis.set_major_locator(MaxNLocator(prune = 'lower')) 
    pylab.savefig('stumpff0to30.eps', format = 'eps') 


    fig3 = pylab.figure() 
    ax3 = fig3.add_subplot(111) 
    ax3.plot(z, y, 'r') 
    ax3.plot(z, y2, 'b') 
    pylab.legend(('$C(z)$', '$S(z)$'), loc = 0) 
    pylab.xlim((0, 500)) 
    pylab.ylim((0, 0.05)) 
    pylab.xlabel('$z$') 
    pylab.gca().xaxis.set_major_locator(MaxNLocator(prune = 'lower')) 
    pylab.savefig('stumpff0to500.eps', format = 'eps') 
    pylab.show() 

然後,您可以使用import stumpff,並且您可以使用stumpff.C(z)stumpff.S(z)。如果你希望能夠在沒有stumpff的情況下使用它們,那麼請使用from stumpff import *from stumpff import C, S

+0

謝謝,我會試一試,然後按照接受它的方式工作。 – dustin

+0

非常感謝! – dustin

相關問題