Python Forum
Calling a function (which accesses a library) from another file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling a function (which accesses a library) from another file
#1
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 :
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
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 :
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()
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!
Reply
#2
you can eliminate the inport of np in tb_ngspice_tools.py, but to do so, you will have to specifically specify where to get np.
so on what is currently line 8, you would need rot = spice.np.linspace(0,1,101)
Personally, I'd import it twice.
Reply
#3
Agree with @Larz60+ Argument rot in wTaper() is a numpy array, so it is very likely that code that uses module ngspice_tools() needs to manipulate numpy arrays and import np. The import statement affects the current namespace: import twice if you need a symbol in two different namespaces. Once a module has been loaded in memory, further import statements cost no more than a simple dictionary lookup.
Reply
#4
Interesting. Is it possible to eliminate the function call from ngspice_tools.py ?
Reply
#5
It is not something to be concerned about. Importing a package is only expensive the first time the package is imported. Subsequent imports don't require reading the module and converting to bytecodes. You can see that here:

module1.py
print("Importing module1.")
module2.py
import module1
print("Importing module2."
program.py
import module1
import module2
Output:
Importing module1. Importing module2.
Notice that module1 is only executed once.

If wTaper needs numpy.exp, don't be concerned that some other file might be importing numpy.

And as to importing numpy in you main program, the only reason to do that is because the program uses numpy.linspace(). Suppose you rewrite ng_spicetools like this:
import numpy as np
 
def parallel(*values):
    y = 0
    for x in values:
        y = y + 1/x
    return 1/y
 
def wTaper(res, start, stop, num):
    # '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

    rot = np.linspace(start, stop, num)
    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, rot
Now your main program no longer needs to import numpy.
import matplotlib.pyplot as plt 
import ngspice_tools as spice
 
r2, r1, rot = spice.wTaper(1000, 0, 1, 101)
plt.plot(rot, r2)
plt.plot(rot,r1)
plt.show()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  calling external function with arguments Wimpy_Wellington 7 1,428 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  [Solved by deanhystad] Create a zip file using zipfile library DZ_Galaxy 2 1,154 Aug-17-2022, 04:57 PM
Last Post: DZ_Galaxy
Sad Iterate randint() multiple times when calling a function Jake123 2 2,042 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Calling a class from a function jc4d 5 1,812 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  How to use a function from third party library? rrowhe4d 2 1,854 Aug-31-2021, 04:30 PM
Last Post: Larz60+
  Override a library function that is not in __all__ Weird 7 3,281 Aug-23-2021, 05:03 PM
Last Post: Larz60+
  [Solved] TypeError when calling function Laplace12 2 2,880 Jun-16-2021, 02:46 PM
Last Post: Laplace12
  calling a function and argument in an input phillup7 3 2,611 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  python library not defined in user defined function johnEmScott 2 3,844 May-30-2020, 04:14 AM
Last Post: DT2000
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,241 Apr-06-2020, 09:14 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020