Fibonacci numbers/program

Alex Martelli aleaxit at yahoo.com
Mon Jan 8 18:02:13 EST 2001


"Eric Anechiarico" <wedjlok at excite.com> wrote in message
news:93d92t$7si$1 at nnrp1.deja.com...
    [snip]
> Thanks for your response, Alex.  I was wondering in what context I
> should use that within the program itself?  I am fairly new to Python
> and have not found very many references in the books that I have read
> that would show the exact sequence on when/where I would put that
> string/command into effect with the code itself.
>
> Basically, I am defining the command to
> def FindFibBetween(A,B), having it go through the simple sequence of
> getting the numbers, and then they output them to a screen.  I want to
> interven so that it will NOT print them to a screen, but dump them
> directly into a text file.

So far, so pretty clear.


> So what I need to know is at what point to I call it in the program to
> open a file, write the numbers to it, and then close the file?
>
> Here is the complete code that I use so far:
>
> def FindFibBetween(A,B):
>   x, y = 1L, 1L
>   while x < B:
>     if x >= A: print x
>     x, y = y, x+y
>
> then I input FindFibBetween(1, 100) or whatever range for (1, n)

Since you wrote this function yourself, changing it may be your
simplest option.  You even get a chance to use the least-popular
recent extension to Python!-)  For example:

def FibBetween(A, B, toFile=None):
    # determine fileobject 'toFile' -- may already be one,
    # or a string that's a filename to be opened; if None,
    # output is to standard output:
    if toFile is not None and not hasattr(toFile,'write'):
        toFile = open(toFile, 'w')
    # and now do the work, outputting appropriately:
    x, y = 1L, 1L
    while x < B:
        if x >= A:
            print >>toFile, x
        x, y = y, x+y

There are basically two parts to this modification.  First,
we've added an optional parameter 'toFile', defaulting to
None (which means: output to standard output), and a
couple of line to determine, if the parameter has been
given, whether it's something with a 'write' attribute (a
file-like object, to use as-is), or not (in which case we'll
assume it's a string-like object, and attempt to open it
for writing; Python will raise suitable exceptions, and end
function execution, if we pass totally unsuitable objects
as the value of the 3rd parameter).

The second mod is very simple, and consists in adding
the (highly-debated) optional ">>toFile," part to the
print statement.  With this part, 'print' will output to
standard output if toFile is None, output to a file-like
object toFile, or raise exception if toFile is neither None
nor a suitable file-like object.


So, now, if you call at the prompt:

>>> FibBetween(1,100)

it will output to standard-output as before; if

>>> FibBetween(1, 100, "fibs.txt")

it will output to a file called fibs.txt in the current
directory (overwriting what it previously contained,
if it was already there); and you can also open a
file (e.g. for appending), or otherwise generate any
file-like object (purvued with a write method) and
pass it yourself as the third parameter.


This is also how you'll integrate this into a GUI: some
input widget in the GUI will determine what kind of
output is desired, and thus the 3rd parameter (others,
of course, will determine the first 2 parameters); if the
output is to be on the GUI, too, a file-like object will
have to be manufactured that will react to '.write' calls
on itself by suitably displaying the strings passed in as the
argument to the .write calls (most GUI's will make that
quite easy, or have suitable pre-cooked output widgets
ready for such use).


Alex






More information about the Python-list mailing list