2013-06-06 96 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

相关问题