Get values from module
I made a program to perform some astronomical calculations. It takes only 6 parameters: latitude , longitude, year, hour, minute and day, because
This I believe there must exist an elegant way to get the job done.
The module looks like this:
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
I need get some var values, so I can add getters functions like:
def f_var_2:
return var_2
But I think this is not the Python way.
Should I define variables or functions instead?
def func_1 (some1, some2):
return some1 + some2
def func_2 (some1, some2):
return func_1 (some1, some2) + some other stuff
Edit to include the code and make (I hope) the right question.
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
In the main program I will have a large matrix (n×m matrix, using list of lists) with the latitude and longitude and the datetime for the matrix. I apply the algorithm to calculate the cosine_zenith_angle for each element in the matrix and some times other calculations are performed.
I'm learning programming so I first made the algorithm implementation, after I put it in a module, put it in a class for encapsulation is the next step. I used the Michalski algorithm for solar position calculation. There are others algorithms and I want to keep the same API if I make other class with another algorithm. That's is the reason to use the "getter" function. I did it this way in other language but I read this is not the Python's way. And I want to learn Python well.
Should I make a function (public) for each intermediate calculation if I want to use it in other module? Should I keep intermediate calculations as they are now I make a getter? There are performance penalties doing intermediate calculations with functions?
Moreover, if I put every calculation in functions, I can't 开发者_高级运维call one of the last ones because it needs the results of other previous. I must modify all functions to accept all the parameters and perform the calculations backward. right? I thought about using a decorator to pass the parameters and the name of the function which produces the desired result. Will it work?
If I understand you correctly, you are doing something like this:
import your_module
your_module.parameter1 = 23
your_module.parameter2 = 46
the_result = your_module.f_var_2
You are setting variables in your module so that some other variables change so that you can get an answer.
You are definitely better off using functions, they were created for this purpose!
import your_module
the_result = your_module.f_var_1(23, 46)
Of course if you need to use the same parameters many different times, you could instead use a class. You could have many instances of that class if the need arises for having different parameters in use at once. Using parameter1 as you have, it would be globally accessible which prevents different parts of your application from using the module as it wants to. Imagine if one part altered the parameter but did not expect another part to do the same.
精彩评论