Jun-07-2023, 04:04 AM
Hello,
I am a new Python user and am trying to create some functions I can use for my own convenience. For now, I am trying to keep these in a separate file.
The following file is called ngspice_tools.py :
You can see on line 19 in the wTaper function, I use the numpy library.
I am calling for example, the function wTaper in my test bench, tb_ngspice_tools.py :
What bothers me is it seems I cannot call wTaper without importing numpy to both ngspice_tools.py and tb_ngspice_tools.py .
I would prefer to only have to import numpy once. Importing numpy in both files seems like wasted overhead, I'm not really sure how the compiler handles that.
Can someone offer me advice on the *correct* way to do this?
I realize at some point I may want to make my various functions into a library, but for now I'm still learning the basics and that seems a little advanced for me.
Thanks!
I am a new Python user and am trying to create some functions I can use for my own convenience. For now, I am trying to keep these in a separate file.
The following file is called ngspice_tools.py :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import numpy as np def parallel( * values): y = 0 for x in values: y = y + 1 / x return 1 / y def wTaper(res, rot): # 'res' is the value of the potentiometer in ohms # 'rot' is rotation of potentiometer as a fraction, eg. rot = 0.1 for 10% rotation # 'rot' is a numpy array a = 0.020264825188570784 b = - 8.509205294369815 c = 0.5136949536968243 # Logistic function r2 = (a * rot + 1 / ( 1 + np.exp(b * (rot - c)))) # Ensure r2(0) = 0 r2 = r2 - r2[ 0 ] # Ensure r2(1) = 1 r2 = r2 / r2[ len (r2) - 1 ] r2 = r2 * res r1 = res - r2 return r2, r1 |
I am calling for example, the function wTaper in my test bench, tb_ngspice_tools.py :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt import scipy as sp import ngspice_tools as spice rot = np.linspace( 0 , 1 , 101 ) r2, r1 = spice.wTaper( 1000 , rot) plt.plot(rot, r2) plt.plot(rot,r1) plt.show() |
I would prefer to only have to import numpy once. Importing numpy in both files seems like wasted overhead, I'm not really sure how the compiler handles that.
Can someone offer me advice on the *correct* way to do this?
I realize at some point I may want to make my various functions into a library, but for now I'm still learning the basics and that seems a little advanced for me.
Thanks!