Of Healthy Programming Practices
I need advice. I'm writing a calculator; I've got the basic functionality of it down, and have finally moved to having it process functions. In this regard, I find the standard library of mathematical functions provided by Python unsatisfactory. While the more commonly used functions are defined, some of the more obscure functions lack proper mention. Examples of this include the arc hyperbolic cotangent, arc hyperbolic cosecant, hyperbolic cosecant, secant, etc. While some of these are just manipulations of the functions provided, it seems the better alternative would be to provide our definitions and refer the program to those when it encounters them. This is where my question lies. Should I just define these functions in the main script, or should I use a separate file? If I the latter, how do I make reference to the separate file? I am relatively new to Python, so if I'm missing a common technique or something, please do let me know. Thanks in advance.
Thisisnotanid
Junior Poster in Training
60 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
You could first install the numpy and scipy modules which contain many special functions.
To answer tour second question, you want to write a separate module containing your own special functions. For example write a mymathfuncs.py file containing
# mymathfuncs.py
import numpy
def arccoth(z):
"""Arc hyperbolic cotangent"""
return numpy.arctanh(1.0/z)
Then store this file in the same directory as your main program, or store it in a directory on your python path and write this in your main program
from mymathfuncs import arccoth
# etc
Also read the part of the python tutorial about modules http://docs.python.org/tutorial/modules.html
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
Small example showing your current level could be usefull to give you correct advice. What books, tutorials have you learned? You have experimented with example code in Python interpreter to really learn the things, have you not? What you think of the code in the code snippet section of Python forum?
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
You could first install the numpy and scipy modules which contain many special functions.
To answer tour second question, you want to write a separate module containing your own special functions. For example write a mymathfuncs.py file containing
# mymathfuncs.py
import numpy
def arccoth(z):
"""Arc hyperbolic cotangent"""
return numpy.arctanh(1.0/z)
Then store this file in the same directory as your main program, or store it in a directory on your python path and write this in your main program
from mymathfuncs import arccoth
# etc
Also read the part of the python tutorial about modules http://docs.python.org/tutorial/modules.html
Thanks Gribouillis
Thisisnotanid
Junior Poster in Training
60 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Small example showing your current level could be usefull to give you correct advice. What books, tutorials have you learned? You have experimented with example code in Python interpreter to really learn the things, have you not? What you think of the code in the code snippet section of Python forum?
thanks pyTony
Thisisnotanid
Junior Poster in Training
60 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0