Second project Euler solution The things I'm interested the most from the review are:


The performance of the code
Overall review of the code structure, styling rules and naming conventions.


Problem: 2


  By considering the terms in the Fibonacci sequence whose values do not
  exceed four million, find the sum of the even-valued terms.


import math
import itertools
#----------------------------------------------------------------------------------
def calc_fibonacci_num(n):
    """Calculates the fibonacci number at the given index.

    Arguments:
        type:&lt;int&gt; - Index value of the fibonacci sequence
    Return:
        type:&lt;int&gt; - Fibonacci number at the given index.
    """
    return int(((1 + math.sqrt(5))**n -(1 - math.sqrt(5))**n)/(2**n*math.sqrt(5)))

#----------------------------------------------------------------------------------
def calc_fibonacci_seq(n):
    """Generates the fibonacci sequence with the highest
    value equal to or less then the number provided by the user.

    Arguments:
        type:&lt;int&gt; - Thrshold value for the fibonacci sequence.
    Return:
        type:&lt;arr&gt; - An array holding the fibonacci sequence.
    """
    return [calc_fibonacci_num(x) for x in itertools.takewhile(lambda x: calc_fibonacci_num(x) &lt;= n, itertools.count())]

#----------------------------------------------------------------------------------
def calc_fibonacci_sum(n, d = 0):
    """Calculates the sum of the numbers in fibonacci sequence.
    Filtering of the numbers to be added is controled with the module operator.

    Arguments:
        type:&lt;int&gt; - Thrshold value for the fibonacci sequence.
        type:&lt;int&gt; - Division value for module operation.
    Return:
        type:&lt;int&gt; - The sum of the numbers in the fibonacci sequence.
    """
    if d &gt; 1 :
        return sum(x for x in calc_fibonacci_seq(n) if x % d == 0)      
    return sum(x for x in calc_fibonacci_seq(n))

#----------------------------------------------------------------------------------
def main():

    print(' Fibonacci-seq : {}'.format(calc_fibonacci_seq(4000000)))
    print(' Fibonacci-sum : {}'.format(calc_fibonacci_sum(4000000, 2)))

#----------------------------------------------------------------------------------
if __name__ == "__main__":
    main()
Your code looks good and is well documented. I have a few suggestions anyway.

Naming

calc_fibonacci_num is a long name for such a function. The calc_ prefix is probably not useful and neither is the _num.

The expression fibonacci(5) is probably explicit enough without additional information.

The same kind of comment applies to the other function names.

Domain definition (and default value)

It seems a bit "un-natural" to handle d=0 as a special case for calc_fibonacci_sum.
It may be clearer to explain that d is expected to be a strictly positive integer.

Then you'd just need to give d the default value 1 and everything would work just fine.

Function called twice

In return [calc_fibonacci_num(x) for x in itertools.takewhile(lambda x: calc_fibonacci_num(x) &lt;= n, itertools.count())], you call the same function with the same argument twice. You can easily check this by adding a print(n) at the beginning of calc_fibonacci_num.

A solution to this issue is to rewrite the function:

def calc_fibonacci_seq(n):
    return itertools.takewhile(lambda x: x &lt;= n, (calc_fibonacci_num(x) for x in itertools.count()))


This fixes another issue you had which is the fact you were building an actual list in memory when you just need a generator to go through the different lists.

An easier way to generate successive fibonacci numbers

The formula you've used is fairly efficient to generate the n-th fibonacci number without generating all the numbers before. However, in our case, we do need to compute them anyway. Thus, a naive solution would probably be faster. I'd suggest the following generator:

def fibo(f=1, g=1):
    """Yields Fibonacci numbers."""
    while True:
        yield f
        f, g = g, f + g