[Tutor] How to import this program, and other questions

Dick Moores rdm at rcblue.com
Mon Jan 22 13:08:43 CET 2007


At 01:56 AM 1/22/2007, ALAN GAULD wrote:

>--- Dick Moores <rdm at rcblue.com> wrote:
>
> > > ...Is the answer to put all 3 functions inside one big
> > > one?
> > >
> > Probably not, its more likely to be a small function
> > that checks its input parameters and calls one of
> > the 3 worker functions.
>
> > Yes. I just realized that I quoted the link wrong. Try
> > <http://www.rcblue.com/Python/clnumDivision_for-web.py>
>
>OK, Now I've seen the code I need a few more
>clues about how you would expose this code?
>
>It seems at first glance as if your main() function
>could just be renamed and made to use parameters and
>it would act as the driver?
>
> > you meant by "a small function that checks its input
> > parameters and calls one of the 3 worker functions."?
>
>I should have said "one or more" but essentially
>your main function does what I meant. If you rename
>it to something more meaningful and add input parameters
>it should be all you need.
>
>Something ike this:
>
>def precision_divide(x,y,digits):
>     s = clnumDiv(x, y, digits)
>     s = numberRounding(s, digits)
>     s = sciNotation(s)
>     return s
>
>Notice I've also made it return s rather than
>print it since thats better practice and more reusable.
>You might want to consider naming x,y
>numerator/denominator to make it more obvious which
>is which. And add a doc string for help() to find.
>
>Then you can just import as usual:
>
>from clnumDivision import precision_divide

Thanks, Alan. Did you see my later reply to myself?

=================================================================
I've been experimenting, and came up with this solution. At least it 
works. I slightly rewrote < 
http://www.rcblue.com/Python/clnumDivision_for-web.py> as clnumDiv.py
(< http://www.rcblue.com/Python/clnumDiv.py>) and put it in
E:\Python25\Lib\site-packages\mine and can now import by
from mine.clnumDiv import clnumDiv
or by
from mine.clnumDiv import clnumDiv as div

I can use this in other scripts, or at the interactive prompt:

from mine.clnumDiv import clnumDiv as div
 >>> x = '1234.5678567856785678567856786586'
 >>> y = '-.00098769876897687654654'
 >>> prec = 50
 >>> div(x, y, prec)
'-1.2499437030427053037075454076437920433253956536740E+006'
 >>>

As I said, this works, but is clnumDiv.py organized correctly pythonically?

Also, I'm still hoping for some suggestions about the details of the code.
=====================================================

So you can see I'd already pretty much implemented what you suggest 
(I think), except for some naming changes, and the doc string. Any 
comments on the code itself? Are there easier ways to do some of the 
things I've done?

As I said, this program is for myself, and I'd like to keep the 
"clnum" string in the function names (I'm writing a bunch of them). 
Using numerator and denominator as arguments in function defs is a 
lot better than x and y, I agree. Or maybe dividend and divisor.

Dick






More information about the Tutor mailing list