import mymodule ## only run one timeself. print("This is my app!") print(mymodule.my_var) mymodule.my_function() #output #This will be executed #This is my app! #10 #This is the function inside the module!
def test_tuple_groups(self): self.assertEqual(mymodule.my_function(1, 2, 3), (1, (2, 3)))
folder = os.path.dirname(os.path.realpath(__file__)) with tempfile.NamedTemporaryFile(suffix='.f90') as f: f.write(source) f.flush() args = ' -c -m {} {} {}'.format(module_name, f.name, extra_args) command = 'cd "{}" && "{}" -c "import numpy.f2py as f2py;f2py.main()" {}'.format(folder, sys.executable, args) status, output = exec_command(command) return status, output, command fortran_source = ''' subroutine my_function(x, y, z) real, intent(in) :: x(:), y(:) real, intent(out) :: z(size(x)) ! using vector operations z(:) = sin(x(:) + y(:)) end subroutine ''' status, output, command = compile_fortran(fortran_source, module_name='mymodule', extra_args="--f90flags='-fopenmp' -lgomp") def empty2darray (foo,i = 10,j = 10): #x = [[foo for i in range(10)] for j in range(10)] #nested list comprehension [x[:] for x in [[foo] * i] * j] return(x) from mymodule import my_function z = None x = "bar" z = "foobar" my_function(x,y,z)
from mymodule import my_function my_function()
args = ' -c -m {} {} {}'.format(module_name, f.name, extra_args) command = 'cd "{}" && "{}" -c "import numpy.f2py as f2py;f2py.main()" {}'.format(folder, sys.executable, args) status, output = exec_command(command) return status, output, command fortran_source = ''' subroutine my_function(x, y, z) real, intent(in) :: x(:), y(:) real, intent(out) :: z(size(x)) ! using vector operations z(:) = sin(x(:) + y(:)) end subroutine ''' with open("frt.f90","w") as f: f.write(fortran_source) with open("frt.f90","r") as f: source=f.read() print(type(source)) status, output, command = compile_fortran(fortran_source, module_name='mymodule', extra_args="--f90flags='-fopenmp' -lgomp") from numpy import f2py #f2py.compile(source,modulename="mymodule",extension=".f90") def empty2darray (foo,i = 10,j = 10): #x = [[foo for i in range(10)] for j in range(10)] #nested list comprehension [x[:] for x in [[foo] * i] * j] return(x) import mymodule z = None x = 1 z = 2 mymodule.my_function(x,y,z)