本文整理匯總了Python中numpy.f2py方法的典型用法代碼示例。如果您正苦於以下問題:Python numpy.f2py方法的具體用法?Python numpy.f2py怎麽用?Python numpy.f2py使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy
的用法示例。
在下文中一共展示了numpy.f2py方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: build_code
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import f2py [as 別名]
def build_code(source_code, options=[], skip=[], only=[], suffix=None,
module_name=None):
"""
Compile and import Fortran code using f2py.
"""
if suffix is None:
suffix = '.f'
with temppath(suffix=suffix) as path:
with open(path, 'w') as f:
f.write(source_code)
return build_module([path], options=options, skip=skip, only=only,
module_name=module_name)
#
# Check if compilers are available at all...
#
示例2: build_code
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import f2py [as 別名]
def build_code(source_code, options=[], skip=[], only=[], suffix=None,
module_name=None):
"""
Compile and import Fortran code using f2py.
"""
if suffix is None:
suffix = '.f'
fd, tmp_fn = tempfile.mkstemp(suffix=suffix)
os.write(fd, asbytes(source_code))
os.close(fd)
try:
return build_module([tmp_fn], options=options, skip=skip, only=only,
module_name=module_name)
finally:
os.unlink(tmp_fn)
#
# Check if compilers are available at all...
#
示例3: calc_info
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import f2py [as 別名]
def calc_info(self):
try:
import numpy.f2py as f2py
except ImportError:
return
f2py_dir = os.path.join(os.path.dirname(f2py.__file__), 'src')
self.set_info(sources=[os.path.join(f2py_dir, 'fortranobject.c')],
include_dirs=[f2py_dir])
return
示例4: _compile
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import f2py [as 別名]
def _compile(self, code: str, path: pathlib.Path, output_folder: pathlib.Path,
module_name: str, **kwargs):
assert isinstance(code, str), type(code)
assert isinstance(path, pathlib.Path), type(path)
assert isinstance(module_name, str), type(module_name)
f2py_args = self.flags('compile') + self.options('compile') \
+ self.f_compiler.options('compile')
f2py_kwargs = {
'f90exec': self.f_compiler.executable('compile'),
'opt': ' '.join(self.f_compiler.flags('compile'))
}
# f2py_kwargs['noopt'] = True
if isinstance(self.f_compiler, PgifortranInterface):
f2py_kwargs['compiler'] = 'unix'
f2py_kwargs['fcompiler'] = 'pg'
extra_args = self.argunparser.unparse(*f2py_args, **f2py_kwargs)
_LOG.info('f2py extra args: %s', extra_args)
_LOG.warning('f2py compiling file: "%s", (%i characters)', path, len(code))
# _LOG.debug('compiled file\'s contents: %s', code)
result = call_tool(
numpy.f2py.compile, kwargs={
'source': code, 'modulename': module_name, 'extra_args': extra_args,
'verbose': False, 'source_fn': '{}'.format(path).replace(' ', '\\ '),
'extension': path.suffix},
cwd=output_folder,
commandline_equivalent='f2py -c -m {} {} "{}"'.format(module_name, extra_args, path),
capture_output=True)
return {'result': result}
示例5: finalize_options
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import f2py [as 別名]
def finalize_options(self):
self.set_undefined_options('build',
('build_base', 'build_base'),
('build_lib', 'build_lib'),
('force', 'force'))
if self.package is None:
self.package = self.distribution.ext_package
self.extensions = self.distribution.ext_modules
self.libraries = self.distribution.libraries or []
self.py_modules = self.distribution.py_modules or []
self.data_files = self.distribution.data_files or []
if self.build_src is None:
plat_specifier = ".%s-%s" % (get_platform(), sys.version[0:3])
self.build_src = os.path.join(self.build_base, 'src'+plat_specifier)
# py_modules_dict is used in build_py.find_package_modules
self.py_modules_dict = {}
if self.f2pyflags:
if self.f2py_opts:
log.warn('ignoring --f2pyflags as --f2py-opts already used')
else:
self.f2py_opts = self.f2pyflags
self.f2pyflags = None
if self.f2py_opts is None:
self.f2py_opts = []
else:
self.f2py_opts = shlex.split(self.f2py_opts)
if self.swigflags:
if self.swig_opts:
log.warn('ignoring --swigflags as --swig-opts already used')
else:
self.swig_opts = self.swigflags
self.swigflags = None
if self.swig_opts is None:
self.swig_opts = []
else:
self.swig_opts = shlex.split(self.swig_opts)
# use options from build_ext command
build_ext = self.get_finalized_command('build_ext')
if self.inplace is None:
self.inplace = build_ext.inplace
if self.swig_cpp is None:
self.swig_cpp = build_ext.swig_cpp
for c in ['swig', 'swig_opt']:
o = '--'+c.replace('_', '-')
v = getattr(build_ext, c, None)
if v:
if getattr(self, c):
log.warn('both build_src and build_ext define %s option' % (o))
else:
log.info('using "%s=%s" option from build_ext command' % (o, v))
setattr(self, c, v)
示例6: build_module
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import f2py [as 別名]
def build_module(source_files, options=[], skip=[], only=[], module_name=None):
"""
Compile and import a f2py module, built from the given files.
"""
code = ("import sys; sys.path = %s; import numpy.f2py as f2py2e; "
"f2py2e.main()" % repr(sys.path))
d = get_module_dir()
# Copy files
dst_sources = []
for fn in source_files:
if not os.path.isfile(fn):
raise RuntimeError("%s is not a file" % fn)
dst = os.path.join(d, os.path.basename(fn))
shutil.copyfile(fn, dst)
dst_sources.append(dst)
fn = os.path.join(os.path.dirname(fn), '.f2py_f2cmap')
if os.path.isfile(fn):
dst = os.path.join(d, os.path.basename(fn))
if not os.path.isfile(dst):
shutil.copyfile(fn, dst)
# Prepare options
if module_name is None:
module_name = get_temp_module_name()
f2py_opts = ['-c', '-m', module_name] + options + dst_sources
if skip:
f2py_opts += ['skip:'] + skip
if only:
f2py_opts += ['only:'] + only
# Build
cwd = os.getcwd()
try:
os.chdir(d)
cmd = [sys.executable, '-c', code] + f2py_opts
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
out, err = p.communicate()
if p.returncode != 0:
raise RuntimeError("Running f2py failed: %s\n%s"
% (cmd[4:], asstr(out)))
finally:
os.chdir(cwd)
# Partial cleanup
for fn in dst_sources:
os.unlink(fn)
# Import
__import__(module_name)
return sys.modules[module_name]