Python 从模块中获取值
我做了一个程序来进行一些天文计算。它只需要6个参数:纬度、经度、年、小时、分钟和天,因为 我相信一定有一种优雅的方式来完成这项工作 该模块如下所示:Python 从模块中获取值,python,Python,我做了一个程序来进行一些天文计算。它只需要6个参数:纬度、经度、年、小时、分钟和天,因为 我相信一定有一种优雅的方式来完成这项工作 该模块如下所示: parameter1 = some1 parameter2 = some2 var_1 = some1 + some2 # or other calcs var_2 = var1 + 'some other calcs' var_n = opertions with some var_i with i < n 但我认为这不是Python的方
parameter1 = some1
parameter2 = some2
var_1 = some1 + some2 # or other calcs
var_2 = var1 + 'some other calcs'
var_n = opertions with some var_i with i < n
但我认为这不是Python的方式
我应该定义变量还是函数
def func_1 (some1, some2):
return some1 + some2
def func_2 (some1, some2):
return func_1 (some1, some2) + some other stuff
编辑以包含代码,并提出(我希望)正确的问题
import math
#values used for algorithm comprobation
#this 6 values will be read from a file
latitude = 35.5
longitude = 59.0833333333
year = 2011
hour = 17
minute = 15
day = 244
local_hour = hour + minute/60.0
#for local time
universal_time = local_hour + 3 #at my location
#for UTC
#universal_time = local_hour
reference_year = 1949
delta = year - reference_year
leap = delta / 4
def julian_day (day, universal_time):
return 2432916.5 + leap + delta * 365 + day + universal_time / 24.0
time = julian_day(day, universal_time) - 2451545.0
mean_longitude = (280.46 + time * 0.9856474) % 360
mean_anomaly = ( 357.528 + 0.9856003 * time ) % 360
ecliptic_lon = (mean_longitude + math.sin(mean_anomaly * math.pi
/180) * 1.915 + 0.02 * math.sin ( mean_anomaly*math.pi/180 * 2.0 ) ) % 360
ecliptic_oblicuity = 23.429 - 0.0000004 * time
num = math.cos(ecliptic_oblicuity * math.pi/180) * math.sin(ecliptic_lon
* math.pi / 180)
den = math.cos (ecliptic_lon * math.pi /180 )
right_ascencion = math.atan (num / den)
if den < 0 :
right_ascencion = (right_ascencion + math.pi)*180/math.pi
elif num < 0 :
right_ascencion = (right_ascencion + math.pi * 2)*180/math.pi
else :
right_ascencion = right_ascencion *180/math.pi
declination = math.asin(math.sin(ecliptic_oblicuity * math.pi / 180) *
math.sin(ecliptic_lon * math.pi / 180)) / math.pi * 180
Greenwich_mean_sidereal_time = (6.697375 + 0.0657098242 * time +
universal_time) % 24
if Greenwich_mean_sidereal_time < 0:
Greenwich_mean_sidereal_time = Greenwich_mean_sidereal_time + 24
local_mean_sidereal_time = ((Greenwich_mean_sidereal_time + longitude / 15) %
24) * 15
if local_mean_sidereal_time - right_ascencion < -180:
hour_angle = local_mean_sidereal_time - right_ascencion + 360
elif local_mean_sidereal_time - right_ascencion > 180:
hour_angle = local_mean_sidereal_time - right_ascencion - 360
else:
hour_angle = local_mean_sidereal_time - right_ascencion
elevation = math.asin(math.sin(declination * math.pi / 180) * math.sin(latitude *
math.pi / 180) + math.cos(declination * math.pi / 180) * math.cos(latitude *
math.pi / 180) * math.cos(hour_angle * math.pi / 180)) / math.pi * 180
azimuth = math.asin(-math.cos(declination * math.pi / 180) * math.sin(
hour_angle * math.pi/180) / math.cos(elevation * math.pi / 180)
) * 180 / math.pi
math.sin(latitude*math.pi/180)))+180/math.pi
azimuth_corrected = 180 - azimuth
zenith_angle = 90.0 - elevation ;
cosine_zenith_angle = math.cos(zenith_angle * math.pi / 180)
def f_cosine_zenith_angle ():
return cosine_zenith_angle
导入数学
#用于算法压缩的值
#这6个值将从文件中读取
纬度=35.5
经度=59.0833333
年份=2011年
小时=17
分钟=15
日=244
本地小时=小时+分钟/60.0
#当地时间
通用时间=本地时间+3#在我的位置
#对于UTC
#通用时间=本地时间
参考年=1949年
delta=年-参考年
leap=增量/4
def朱利安日(日,世界时):
返回2432916.5+跳跃+增量*365+天+通用时间/24.0
时间=朱利安日(日,世界时间)-2451545.0
平均经度=(280.46+时间*0.9856474)%360
平均异常=(357.528+0.9856003*时间)%360
黄道经度=(平均经度+math.sin(平均异常*math.pi
/180)*1.915+0.02*math.sin(平均异常*math.pi/180*2.0))%360
黄道倾角=23.429-0.0000004*时间
num=math.cos(黄道倾角*math.pi/180)*math.sin(黄道倾角)
*math.pi/180)
den=math.cos(黄道长*math.pi/180)
右上升=math.atan(num/den)
如果den<0:
右上升=(右上升+math.pi)*180/math.pi
elif num<0:
右上升=(右上升+math.pi*2)*180/math.pi
其他:
右上升=右上升*180/math.pi
赤纬=数学asin(数学sin(黄道倾角*math.pi/180)*
math.sin(黄道长*math.pi/180))/math.pi*180
格林威治恒星平均时间=(6.697375+0.0657098242*时间+
通用时间)%24
如果格林威治平均恒星时间<0:
格林威治平均恒星时间=格林威治平均恒星时间+24
本地平均恒星时间=((格林威治平均恒星时间+经度/15)%)
24) * 15
如果本地平均恒星时间-右上升<-180:
小时角=本地平均恒星时间-右上升+360
elif局部平均恒星时间-右上升>180:
小时角=本地平均恒星时间-右上升-360
其他:
小时角=本地平均恒星时间-右上升
高程=数学asin(数学sin(赤纬*数学pi/180)*数学sin(纬度*
math.pi/180)+math.cos(赤纬*math.pi/180)*math.cos(纬度*
math.pi/180)*math.cos(小时角*math.pi/180))/math.pi*180
方位角=数学asin(-math.cos(偏角*math.pi/180)*math.sin(
小时角*math.pi/180)/math.cos(标高*math.pi/180)
)*180/math.pi
math.sin(纬度*math.pi/180))+180/math.pi
方位角校正=180-方位角
天顶角=90.0-标高;
余弦天顶角=math.cos(天顶角*math.pi/180)
定义f_余弦_天顶角():
返回余弦天顶角
在主程序中,我将有一个大矩阵(n×m矩阵,使用列表列表),其中包含矩阵的纬度、经度和日期时间。我应用该算法计算矩阵中每个元素的余弦天顶角,有时还会执行其他计算
我在学习编程,所以我首先做了算法的实现,在我把它放到一个模块中之后,下一步就是把它放到一个类中进行封装。我使用米查尔斯基算法计算太阳位置。还有其他算法,如果我用另一个算法创建其他类,我希望保持相同的API。这就是使用“getter”函数的原因。我用其他语言这样做,但我读到这不是Python的方式。我想学好Python
如果我想在其他模块中使用,是否应该为每个中间计算设置一个函数(公共)?我应该保持中间的计算,因为他们现在我做一个getter?使用函数进行中间计算时会有性能损失
此外,如果我把每一个计算都放在函数中,我就不能调用最后的一个,因为它需要其他先前计算的结果。我必须修改所有函数以接受所有参数并反向执行计算。正当我考虑使用一个装饰器来传递参数和函数名,从而产生所需的结果。这行得通吗?如果我没弄错的话,你正在做这样的事情:
import your_module
your_module.parameter1 = 23
your_module.parameter2 = 46
the_result = your_module.f_var_2
您正在设置模块中的变量,以便更改其他一些变量,以便您可以得到答案
使用函数肯定会更好,它们就是为此而创建的
import your_module
the_result = your_module.f_var_1(23, 46)
当然,如果需要多次使用相同的参数,可以使用类。如果需要同时使用不同的参数,则可以有该类的多个实例。使用现有的parameter1,它将是全局可访问的,这将阻止应用程序的不同部分按其希望的方式使用模块。想象一下,如果一个部件改变了参数,但不希望另一个部件也这样做。请发布正在进行天文计算的实际程序。如果没有更多的上下文,我无法理解你的问题。永远不要用泛型名称命名变量和函数,如
var2
或func10
,它们使调试几乎不可能。在任何事情正常工作之前,还担心性能损失是一个问题。感谢Jarrod的建议。我只是想在问题上简洁一些,这是错误的想法。代码不正确。请参阅孤立行math.sin(latitude*math.pi/180))+180/math.pi
谢谢您的回答。我担心大量函数调用的成本。几乎不需要任何时间。很抱歉,我没有回答清楚我的问题。在您的示例中,可能是_result=your_module.f_var_5()
import your_module
the_result = your_module.f_var_1(23, 46)