从modu获取值

2024-10-04 01:34:53 发布

您现在位置: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

我需要获取一些var值,这样我就可以添加getters函数,比如:

^{pr2}$

但我认为这不是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

在主程序中,我将有一个大矩阵(n×m矩阵,使用列表列表),其中包含经纬度和日期时间。我用这个算法来计算矩阵中每个元素的余弦天顶角,有时还会进行其他计算。在

我是学编程的,所以我首先做了算法实现,之后我把它放到一个模块里,放到一个类中进行封装是下一步。我用Michalski算法计算太阳位置。还有其他算法,如果我用另一个算法生成其他类,我希望保持相同的API。这就是使用“getter”函数的原因。我用其他语言这样做,但我读到这不是Python的方式。我想学好Python。在

如果要在其他模块中使用,是否应该为每个中间计算创建一个函数(public)?我是否应该保持中间计算,就像现在我做一个吸气剂?用函数进行中间计算会有性能损失吗?在

而且,如果我把每一个计算都放到函数中,我就不能调用最后一个,因为它需要其他前一个的结果。我必须修改所有函数以接受所有参数并反向执行计算。正确的?我考虑过使用一个修饰符来传递参数和产生所需结果的函数的名称。能用吗?在


Tags: 函数righttimelocalpimathsincos
1条回答
网友
1楼 · 发布于 2024-10-04 01:34:53

如果我没听错,你是在做这样的事:

import your_module
your_module.parameter1 = 23
your_module.parameter2 = 46
the_result = your_module.f_var_2

您正在模块中设置变量,以便其他一些变量发生更改,以便您能够得到答案。在

你最好使用函数,它们是为这个目的而创建的!在

^{pr2}$

当然,如果需要多次使用相同的参数,则可以使用类。如果需要同时使用不同的参数,则可以有该类的多个实例。使用现有的parameter1,它将是全局可访问的,这将阻止应用程序的不同部分按其希望使用模块。想象一下,如果一个部件改变了参数,但不希望另一个部件也这样做。在

相关问题 更多 >