本文整理汇总了Python中openmdao.util.dep.PythonSourceTreeAnalyser类的典型用法代码示例。如果您正苦于以下问题:Python PythonSourceTreeAnalyser类的具体用法?Python PythonSourceTreeAnalyser怎么用?Python PythonSourceTreeAnalyser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PythonSourceTreeAnalyser类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_optproblem_list
def build_optproblem_list(include=[], exclude=[]):
"""builds a list of optproblems
include: (optional) list of optproblems names
The names of the optproblems to test. Only optproblems in this list
will be tested. Each name should be just the class name (e.g., 'SellarProblem').
Must be set to None, if excludes is specified. If not specified,
all OptProblems, except those in exclude are used.
exclude: (optional) list of optproblems names
The names of the optproblems not to test. All optproblems from
openmdao.lib.optproblems will be tested, except for the ones in this
list. Each name should just be the class name (e.g. 'SellarProblem').
Must be set to None, if includes is specified.
"""
if include and exclude:
raise ValueError("Can't set both include and exlude for OptProblems")
startdirs = [os.path.dirname(openmdao.lib.optproblems.__file__),
os.path.dirname(openmdao.main.__file__)]
psta = PythonSourceTreeAnalyser(startdirs, os.path.join('*','test','*'))
opt_problems = psta.find_inheritors("openmdao.main.problem_formulation.OptProblem")
probs = []
for prob_name in opt_problems:
prob_class = prob_name.split(".")[-1]
prob_package = ".".join(prob_name.split(".")[:-1])
if (not include and not exclude) or (include and prob_class in include) or \
(exclude and prob_class not in exclude):
prob_package = __import__(prob_package,globals(),locals(),[prob_class,],-1)
probs.append(getattr(prob_package,prob_class)()) #create instance of the OptProblem
return probs
示例2: _find_all_plugins
def _find_all_plugins(searchdir):
"""Return a dict containing lists of each plugin type found, keyed by
plugin group name, e.g., openmdao.component, openmdao.variable, etc.
"""
dct = {}
modnames = ['openmdao.main',
'openmdao.lib.datatypes',
'openmdao.lib.components',
'openmdao.lib.drivers',
'openmdao.lib.surrogatemodels',
'openmdao.lib.doegenerators',
'openmdao.lib.differentiators',
'openmdao.lib.optproblems',
'openmdao.lib.casehandlers',
'openmdao.lib.architectures']
modules = []
for mod in modnames:
try:
__import__(mod)
except ImportError:
print 'skipping import of %s' % mod
else:
modules.append(sys.modules[mod])
dirs = [os.path.dirname(m.__file__) for m in modules]+[searchdir]
psta = PythonSourceTreeAnalyser(dirs, exclude=_exclude_funct)
for key, val in plugin_groups.items():
dct[key] = set(psta.find_inheritors(val))
return dct
示例3: build_arch_list
def build_arch_list(include=[], exclude=[]):
"""Builds a list of architectures.
include: list of architecture names
The names of the architectures to test. Only architectures in this list
will be tested. Each name should be just the class name (e.g. 'MDF', 'CO').
Must be set to None, if excludes is specified
exclude: list of architecture names
The names of the architectures not to test. All architectures from
openmdao.lib.architectures will be tested, except for the ones in this
list. Each name should be just the class name (e.g., 'MDF', 'CO').
Must be set to None, if includes is specified.
"""
if include and exclude:
raise ValueError("Can't set both include and exlude")
startdirs = [os.path.dirname(openmdao.lib.architectures.__file__),
os.path.dirname(openmdao.main.__file__)]
psta = PythonSourceTreeAnalyser(startdirs, os.path.join('*','test','*'))
architectures = psta.find_inheritors("openmdao.main.arch.Architecture")
archs = []
for arch_name in architectures:
arch_class = arch_name.split(".")[-1]
arch_package = ".".join(arch_name.split(".")[:-1])
if (not include and not exclude) or (include and arch_class in include) or \
(exclude and arch_class not in exclude):
arch_package = __import__(arch_package,globals(),locals(),[arch_class,],-1)
archs.append(getattr(arch_package,arch_class)()) #create instance of the Architecture
return archs
示例4: test_optproblems_solution
def test_optproblems_solution(self):
# test to make sure that at the specified solution point, the objective
# values match what is given in the solution
#find all the optproblems in lib
startdirs = [os.path.dirname(openmdao.lib.optproblems.__file__),]
psta = PythonSourceTreeAnalyser(startdirs, os.path.join('*','test','*'))
opt_problems = psta.find_inheritors("openmdao.main.problem_formulation.OptProblem")
for prob_name in opt_problems:
#print "running %s"%prob_name
prob_class = prob_name.split(".")[-1]
prob_package = ".".join(prob_name.split(".")[:-1])
prob_package = __import__(prob_package,globals(),locals(),[prob_class,],-1)
prob = getattr(prob_package,prob_class)() #create instance of the OptProblem
prob = set_as_top(prob)
try:
prob.check_solution(strict=True)
except ValueError as err:
self.fail("There is missing piece of the solution for %s%s"%(prob.__class__,str(err)))
prob.architecture = OptProblemSolutionCheck()
prob.run()
error = prob.check_solution(strict=True)
self.assertAccuracy(prob_name,error,.001)
示例5: test_PythonSourceTreeAnalyser
def test_PythonSourceTreeAnalyser(self):
try:
import openmdao.main
import openmdao.lib
except ImportError:
# don't perform this test if openmdao.main
# and openmdao.lib aren't present
raise SkipTest("this test requires openmdao.main and openmdao.lib")
def exclude_tests(pname):
parts = pname.split(os.sep)
return 'test' in parts
startdirs = [os.path.dirname(openmdao.main.__file__),
os.path.dirname(openmdao.lib.__file__)]
psta = PythonSourceTreeAnalyser(startdirs, exclude_tests)
self.assertTrue('openmdao.main.component.Component' in
psta.graph['openmdao.main.container.Container'])
self.assertTrue('openmdao.main.assembly.Assembly' in
psta.graph['openmdao.main.component.Component'])
self.assertTrue('openmdao.main.datatypes.float.Float' in
psta.graph['openmdao.main.variable.Variable'])
comps = psta.find_inheritors('openmdao.main.component.Component')
icomps = psta.find_inheritors('IComponent')
self.assertTrue('openmdao.main.assembly.Assembly' in icomps)
comps.extend(psta.find_inheritors('openmdao.main.variable.Variable'))
comps.extend(psta.find_inheritors('enthought.traits.api.Array'))
comps = [x.rsplit('.',1)[1] for x in comps if '.examples.' not in x and '.optproblems.' not in x]
cset = set(comps)
excludes = set([
'Driver',
'DriverUsesDerivatives',
'DistributionCaseDriver',
'CaseIterDriverBase',
'PassthroughTrait',
'PassthroughProperty',
'OptProblem',
'TraitArray',
'Broadcast', # utility class for bliss2000
'SubSystemOpt', # utility class for bliss2000
'SubSystemObj' # utility class for bliss2000
])
cset = cset - excludes
from openmdao.main.api import get_available_types
types = set([x[0] for x in get_available_types()])
types = [x.rsplit('.',1)[1] for x in types if x.startswith('openmdao.')]
tset = set(types)
noentrypts = cset-tset
if noentrypts:
self.fail("the following Components are not registered using entry points: %s" % noentrypts)
示例6: find_all_plugins
def find_all_plugins(searchdir):
"""Return a dict containing lists of each plugin type found, keyed by
plugin group name, e.g., openmdao.component, openmdao.variable, etc.
"""
dct = {}
psta = PythonSourceTreeAnalyser(searchdir, exclude=_exclude_funct)
for key, lst in plugin_groups.items():
epset = set(psta.find_inheritors(lst[0]))
if epset:
dct[key] = epset
return dct
示例7: test_PythonSourceTreeAnalyser
def test_PythonSourceTreeAnalyser(self):
def exclude(pname):
keywords = set(["test", "docs", "examples", "optproblems"])
parts = set(pname.split(os.sep))
return keywords.intersection(parts)
psta = PythonSourceTreeAnalyser(self.startdirs, exclude)
self.assertTrue("openmdao.main.component.Component" in psta.graph["openmdao.main.container.Container"])
self.assertTrue("openmdao.main.assembly.Assembly" in psta.graph["openmdao.main.component.Component"])
self.assertTrue("openmdao.main.datatypes.float.Float" in psta.graph["openmdao.main.variable.Variable"])
comps = psta.find_inheritors("openmdao.main.component.Component")
icomps = psta.find_inheritors("IComponent")
self.assertTrue("openmdao.main.assembly.Assembly" in icomps)
comps.extend(psta.find_inheritors("openmdao.main.variable.Variable"))
comps.extend(psta.find_inheritors("enthought.traits.api.Array"))
comps = [x.rsplit(".", 1)[1] for x in comps]
# comps = [x.rsplit('.',1)[1] for x in comps if '.examples.' not in x and '.optproblems.' not in x]
cset = set(comps)
excludes = set(
[
"Driver",
"DriverUsesDerivatives",
"DistributionCaseDriver",
"CaseIterDriverBase",
"PassthroughTrait",
"PassthroughProperty",
"OptProblem",
"TraitArray",
"Broadcast", # utility class for bliss2000
"SubSystemOpt", # utility class for bliss2000
"SubSystemObj", # utility class for bliss2000
]
)
cset = cset - excludes
from openmdao.main.api import get_available_types
types = set([x[0] for x in get_available_types()])
types = [x.rsplit(".", 1)[1] for x in types if x.startswith("openmdao.")]
tset = set(types)
noentrypts = cset - tset
if noentrypts:
self.fail("the following Components are not registered using entry points: %s" % noentrypts)
示例8: test_PythonSourceTreeAnalyser
def test_PythonSourceTreeAnalyser(self):
skipdirs = set(['test', 'docs', 'examples', 'optproblems',
'build', 'dist'])
psta = PythonSourceTreeAnalyser(self.startdirs,
direxclude=lambda d: d in skipdirs)
self.assertTrue('openmdao.main.component.Component' in
psta.graph['openmdao.main.container.Container'])
self.assertTrue('openmdao.main.assembly.Assembly' in
psta.graph['openmdao.main.component.Component'])
self.assertTrue('openmdao.main.datatypes.float.Float' in
psta.graph['openmdao.main.variable.Variable'])
comps = psta.find_inheritors('openmdao.main.component.Component')
icomps = psta.find_inheritors('IComponent')
self.assertTrue('openmdao.main.assembly.Assembly' in icomps)
comps.extend(psta.find_inheritors('openmdao.main.variable.Variable'))
comps.extend(psta.find_inheritors('traits.api.Array'))
comps = [x.rsplit('.',1)[1] for x in comps]
cset = set(comps)
excludes = set([
'Driver',
'DriverUsesDerivatives',
'DistributionCaseDriver',
'CaseIterDriverBase',
'PassthroughTrait',
'PassthroughProperty',
'OptProblem',
'TraitArray',
'Broadcast', # utility class for bliss2000
'SubSystemOpt', # utility class for bliss2000
'SubSystemObj' # utility class for bliss2000
])
cset = cset - excludes
from openmdao.main.api import get_available_types
types = set([x[0] for x in get_available_types()])
types = [x.rsplit('.',1)[1] for x in types if x.startswith('openmdao.')]
tset = set(types)
noentrypts = cset-tset
if noentrypts:
self.fail("the following Components are not registered using entry points: %s" % noentrypts)
示例9: __init__
def __init__(self, groups=plugin_groups.keys(), search_path=None):
super(PkgResourcesFactory, self).__init__()
self._have_new_types = True
self._groups = copy.copy(groups)
self._search_path = search_path
self.env = Environment(search_path)
self.tree_analyser = PythonSourceTreeAnalyser()
示例10: _find_all_plugins
def _find_all_plugins(searchdir):
"""Return a dict containing lists of each plugin type found, keyed by
plugin group name, e.g., openmdao.component, openmdao.variable, etc.
"""
dct = {}
psta = PythonSourceTreeAnalyser(searchdir)
comps = psta.find_inheritors('openmdao.main.component.Component')
comps.extend(psta.find_inheritors('openmdao.main.api.Component'))
comps = set(comps)
drivers = psta.find_inheritors('openmdao.main.driver.Driver')
drivers.extend(psta.find_inheritors('openmdao.main.api.Driver'))
drivers = set(drivers)
comps = comps - drivers
dct['openmdao.component'] = comps
dct['openmdao.driver'] = drivers
variables = psta.find_inheritors('openmdao.main.api.Variable')
variables.extend(psta.find_inheritors('openmdao.main.variable.Variable'))
dct['openmdao.variable'] = set(variables)
return dct
示例11: test_PythonSourceTreeAnalyser
def test_PythonSourceTreeAnalyser(self):
try:
import openmdao.main
import openmdao.lib
except ImportError:
# don't perform this test if openmdao.main
# and openmdao.lib aren't present
raise SkipTest("this test requires openmdao.main and openmdao.lib")
startdirs = [os.path.dirname(openmdao.main.__file__),
os.path.dirname(openmdao.lib.__file__)]
psta = PythonSourceTreeAnalyser(startdirs, os.path.join('*','test','*'))
self.assertTrue('openmdao.main.component.Component' in
psta.graph['openmdao.main.container.Container'])
self.assertTrue('openmdao.main.assembly.Assembly' in
psta.graph['openmdao.main.component.Component'])
self.assertTrue('openmdao.lib.datatypes.float.Float' in
psta.graph['openmdao.main.variable.Variable'])
comps = psta.find_inheritors('openmdao.main.component.Component')
comps.extend(psta.find_inheritors('openmdao.main.variable.Variable'))
comps.extend(psta.find_inheritors('enthought.traits.api.Array'))
comps = [x.rsplit('.',1)[1] for x in comps]
comps.remove('Driver')
comps.remove('DriverUsesDerivatives')
comps.remove('CaseIterDriverBase')
comps.remove('PassthroughTrait')
comps.remove('PassthroughProperty')
from openmdao.main.api import get_available_types
groups = [ 'openmdao.component',
'openmdao.driver',
'openmdao.variable']
types = set([x[0] for x in get_available_types(groups)])
types = [x.rsplit('.',1)[1] for x in types if x.startswith('openmdao.')]
cset = set(comps)
tset = set(types)
noentrypts = cset-tset
if noentrypts:
self.fail("the following Components are not registered using entry points: %s" % noentrypts)
示例12: test_optproblems_solution
def test_optproblems_solution(self):
#find all the optproblems in lib
startdirs = [os.path.dirname(openmdao.lib.optproblems.__file__),]
psta = PythonSourceTreeAnalyser(startdirs, os.path.join('*','test','*'))
opt_problems = psta.find_inheritors("openmdao.main.problem_formulation.OptProblem")
for prob_name in opt_problems:
#print "running %s"%prob_name
prob_class = prob_name.split(".")[-1]
prob_package = ".".join(prob_name.split(".")[:-1])
prob_package = __import__(prob_package,globals(),locals(),[prob_class,],-1)
prob = getattr(prob_package,prob_class)() #create instance of the OptProblem
prob.architecture = OptProblemSolutionCheck()
prob.configure()
prob.run()
error = prob.check_solution(strict=True)
self.assertAccuracy(prob_name,error,.001)
示例13: PkgResourcesFactory
class PkgResourcesFactory(Factory):
"""A Factory that loads plugins using the pkg_resources API, which means
it searches through egg info of distributions in order to find any entry
point groups corresponding to openmdao plugin types, e.g.,
openmdao.component, openmdao.variable, etc.
"""
def __init__(self, groups=plugin_groups.keys(), search_path=None):
super(PkgResourcesFactory, self).__init__()
self._have_new_types = True
self._groups = copy.copy(groups)
self._search_path = search_path
self.env = Environment(search_path)
self.tree_analyser = PythonSourceTreeAnalyser()
def create(self, typ, version=None, server=None,
res_desc=None, **ctor_args):
"""Create and return an object of the given type, with
optional name, version, server id, and resource description.
"""
if server is not None or res_desc is not None:
return None
klass = self._load(typ, version)
if klass is None:
return None
else:
return klass(**ctor_args)
def _load(self, typ, version):
"""Return class for *typ* and *version*."""
classes = self._get_type_dict()
try:
lst = classes[typ]
dist = lst[0]
groups = lst[1]
klass = dist.load_entry_point(groups[0], typ)
if version is not None and dist.version != version:
return None
return klass
except KeyError:
if self._search_path is None:
return None
# try to look in the whole environment
for group in self._groups:
for proj in self.env:
for dist in self.env[proj]:
if version is not None and version != dist.version:
continue
ep = dist.get_entry_info(group, typ)
if ep is not None:
dist.activate()
klass = ep.load(require=True, env=self.env)
self._have_new_types = True
return klass
if version is None:
# newest version didn't have entry point, so skip to next project
break
return None
def _entry_map_info(self, distiter):
dct = {}
for group in plugin_groups.keys():
for dist in distiter:
for name, value in dist.get_entry_map(group).items():
lst = dct.setdefault(name, (dist, [], set()))
lst[1].append(group)
lst[2].add(value.module_name)
return dct
def _get_type_dict(self):
if self._have_new_types:
self._entry_pt_classes = self._entry_map_info(working_set)
return self._entry_pt_classes
def _get_meta_info(self, typ_list, groups, typ_dict):
distset = set()
for name, lst in typ_dict.items():
dist = lst[0]
modules = lst[2]
distset.add(dist.project_name)
ifaces = set()
for g in lst[1]:
ifaces.update(plugin_groups[g])
meta = {
'version': dist.version,
'ifaces': set(ifaces),
}
for modname in modules:
fpath = find_module(modname)
if fpath is not None:
fanalyzer = self.tree_analyser.analyze_file(fpath, use_cache=True)
meta['bases'] = fanalyzer.classes[name].bases
meta['ifaces'].update(fanalyzer.classes[name].meta['ifaces'])
meta['ifaces'] = list(meta['ifaces'])
if groups.intersection(lst[1]):
typ_list.append((name, meta))
self.tree_analyser.flush_cache()
#.........这里部分代码省略.........