本文整理汇总了Python中importer.Importer.load方法的典型用法代码示例。如果您正苦于以下问题:Python Importer.load方法的具体用法?Python Importer.load怎么用?Python Importer.load使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类importer.Importer
的用法示例。
在下文中一共展示了Importer.load方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_importer
# 需要导入模块: from importer import Importer [as 别名]
# 或者: from importer.Importer import load [as 别名]
def get_importer(config):
importer = Importer()
importer.load(
dict(key='service_classes', module_names=config.SERVICES, class_name='Service'),
) # load all modules here
services = []
for service_class in importer.service_classes:
srv = service_class()
srv.name = service_class.__module__
services.append(srv)
importer.services = services
return importer
示例2: AppTestCase
# 需要导入模块: from importer import Importer [as 别名]
# 或者: from importer.Importer import load [as 别名]
class AppTestCase(TestCase):
def setUp(self):
super(AppTestCase, self).setUp()
self.server_parameters = ServerParameters(
io_loop=self.io_loop,
host='localhost',
port=8888,
config_path='./tests/fixtures/test-valid.conf',
log_level='INFO',
debug=True,
)
self.config = Config()
self.importer = Importer()
self.importer.load(
dict(key='service_classes', module_names=self.config.SERVICES, class_name='Service'),
) # load all modules here
services = []
for service_class in self.importer.service_classes:
srv = service_class()
srv.name = service_class.__module__
services.append(srv)
self.importer.services = services
self.context = Context(self.server_parameters, self.config, self.importer)
@gen_test
async def test_can_create_app(self):
app = await LevelApp.create(self.context)
expect(app).not_to_be_null()
expect(app.context).to_equal(self.context)
@gen_test
async def test_can_initialize_services(self):
class TestService(BaseService):
def __init__(self, *args, **kw):
super(TestService, self).__init__(*args, **kw)
self.initialized = False
self.name = 'TestService'
self.app = None
async def initialize_service(self, app):
await super(TestService, self).initialize_service(app)
self.initialized = True
s = TestService()
self.context.importer.services = [s]
app = LevelApp(self.context, [])
expect(app).not_to_be_null()
await app.initialize()
expect(s.initialized).to_be_true()
expect(s.app).to_equal(app)
@gen_test
async def test_can_get_handlers_from_services(self):
class TestService(BaseService):
def __init__(self):
self.initialized = False
self.name = 'TestService'
self.app = None
async def initialize_service(self, app):
await super(TestService, self).initialize_service(app)
self.initialized = True
async def get_handlers(self):
return (
('/test', None),
)
s = TestService()
self.context.importer.services = [s]
app = LevelApp(self.context, [])
expect(app).not_to_be_null()
handlers = await app.get_handlers()
expect(handlers).to_length(2)
expect(handlers[1]).to_be_like(
('/test', None),
)
示例3: __init__
# 需要导入模块: from importer import Importer [as 别名]
# 或者: from importer.Importer import load [as 别名]
class Solver:
def __init__(self, options):
self.importer = Importer()
# try read all the files in the dir
if options.dir_name is not None:
self._files = [f for f in self.get_files(options.dir_name)]
else:
self._files = [options.file_name] # only one file
self._algo_name = options.algorithm if (options.algorithm is not None) else 'bfs'
self.options = options
def get_algo(self, name):
"""Return an instance of the algo specified"""
name = name.lower()
if name == 'bfs':
return BestFirstSearch
elif name in ['hillclimbing','hillclimb']:
return HillClimbing
elif name == 'hillclimb-restart':
return RestartingHillClimb
elif name in ['simanneal', 'simulated-annealing','annealing']:
return SimulatedAnnealing
def get_options(self):
"""Pull out the relevant (valid) options"""
return {
'alpha':float(self.options.alpha),
'iterations':int(self.options.iterations),
'temp':float(self.options.temperature),
'restarts':int(self.options.restarts),
'operator':str(self.options.operator),
}
def get_files(self, path):
"""Find all solvable files in a dir"""
for top, dirs, files in os.walk('problems'):
for nm in files:
file_name = os.path.join(top, nm)
if file_name[-4:] == '.txt':
yield file_name
def solve_file(self, file_name):
"""Attempt to solve the file, with the given algo"""
tour = self.importer.load(file_name)
try:
algo = self.get_algo(self._algo_name)
algo = algo(tour, options=self.get_options())
except Exception as e:
logger.exception('No such algorithm %s' % self._algo_name)
raise e
logger.info("Loaded problem '%s' and algo '%s'" % (file_name, algo.NAME))
path = algo.solve()
length = tour.get_length(path)
logger.info('Path found for %s. Cost: %r' % (tour.name,length))
# print path
return tour.name, path, length
def run(self):
"""Run the solver over all provided files"""
try:
logger.info('Travelling Salesman Problem')
results = []
for file_name in self._files:
results.append( self.solve_file(file_name) )
for name, path, length in results:
print 'FILE=%s,' % name
print 'SIZE=%d,' % length
print 'PATH=%s' % ','.join(map(str,path))
# logger.info('%s\t%s' % (file_name.split('/')[1], length))
except Exception as e:
logging.info('------------------------------')
logging.info('Encountered an error. Halting.')
logging.exception(e)