0

Using python 3.3

Stumbled upon another problem with my program. Its the same solar program. Again i decided to add more functionality. Its basically ad-hoc. I'm adding things as i go along. I realize it can be made more efficient but once i decide its done, I'll post the whole coding up.

Anyway, i need to add the results from multiple functions. Here's a part of my coding:

def janCalc():
    for a in angle(0,360,10):                                           #angle of orientation
        for d in days(1,32,1.0006630137):                               #day number of year
            for smodule in equation():                  #equation() function not shown in this coding
                total_jan+=smodule              #total_jan is already defined elsewhere              
        avg_jan=total_jan/(60*(1.0006630137*31))                
        ratio_jan=avg_jan/5.67
        calcJan=(ratio_jan*4.79)
        yield calcJan
        total_jan=0                     #necessary to reset total to 0 for next angle interval    

def febCalc():
    for a in angle(0,360,10):
        for d in days ((1.0006630137*31),61,1.0006630137):
            for smodule in equation():
                total_feb+=smodule
        avg_feb=total_feb/(60*(1.0006630137*28))
        ratio_feb=avg_feb/6.56
        calcFeb=(ratio_feb*4.96)
        yield calcFeb
        total_feb=0

#etc..............

Is there anyway to add the yield of each function? for e.g: calcJan+calcFeb+.....

I would like to get the total results under each angle interval and then dividing by 12 to get the average value per interval. Like so:-

0 degrees---->total/12

10 deg ---->total/12

20 deg ---->total/12

30 deg ---->total/12

........

360 deg ---->total/12

If you need more info, let me know.

ADDENDUM

The solution was essentially solved by @jonrsharpe. But i encountered a bit of a problem.

Traceback (most recent call last):
  File "C:\Users\User\Documents\Python\Solar program final.py", line 247, in <module>
    output=[sum(vals)/12 for vals in zip(*(gen() for gen in months))]
  File "C:\Users\User\Documents\Python\Solar program final.py", line 247, in <listcomp>
    output=[sum(vals)/12 for vals in zip(*(gen() for gen in months))]
  File "C:\Users\User\Documents\Python\Solar program final.py", line 103, in janCalc
    for smodule in equation():
  File "C:\Users\User\Documents\Python\Solar program final.py", line 63, in equation
    d=math.asin(math.sin(math.radians(23.45))*math.sin(math.radians((360/365.242)*(d-81))))
NameError: global name 'd' is not defined

I've isolated it to:

for d in days ((1.0006630137*31),61,1.0006630137):    
    for smodule in equation(): 

It turns out i can't reference a function from inside a function? I'm not too sure. So even my original coding did not work. I assumed it was working because previously i had not defined each month as a function. I should have tested it out first.

Do you know how to get around this?

6
  • What does "I only know how to get the yield per function but don't know how to interact them in the manner i want." mean? Also, try to factor out all those "magic numbers" in your code. Commented Apr 20, 2014 at 7:36
  • I shouldn't have included that. It is confusing. What i meant to say was, I just know how to refer to one function, but not how to refer to multiple functions for my equation. I hope that makes sense. Commented Apr 22, 2014 at 8:50
  • I think the problem is that you don't define total_feb; you certainly can "reference a function from inside a function". Where is the full error traceback? Commented Apr 22, 2014 at 20:18
  • just posted the traceback error. Commented Apr 22, 2014 at 20:27
  • The problem is that that function equation doesn't work; it's unrelated to the code you have posted so far! You should test the function separately before calling it from other code; you may need to open a new question (with all the appropriate information) if you can't make it work separately. First I would suggest you get rid of any global variables and pass functions the arguments they need explicitly, this will make NameErrors easier to find and fix. Commented Apr 22, 2014 at 20:32

1 Answer 1

1

A simple example to demonstrate how to combine multiple generators:

>>> def gen1():
    for x in range(5):
        yield x

>>> def gen2():
    for x in range(5, 10):
        yield x

>>> [sum(vals) for vals in zip(*(gen() for gen in (gen1, gen2)))]
[5, 7, 9, 11, 13]

Or, written out long hand:

output = list(gen1())
for index, value in enumerate(gen2()):
    output[index] += value

You can modify either version to include a division, too, so your case would look something like:

months = [janCalc, fabCalc, ...]

output = [sum(vals) / 12 for vals i zip(*(gen() for gen in months))]
Sign up to request clarification or add additional context in comments.

1 Comment

Hey jon, sorry for bothering you again. i encountered abit of a problem. Your coding is fine. But it is on my part that is faulty. I've made an addendum to the problem. please check it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.