本文整理汇总了Python中safe.impact_functions.registry.Registry.get_instance方法的典型用法代码示例。如果您正苦于以下问题:Python Registry.get_instance方法的具体用法?Python Registry.get_instance怎么用?Python Registry.get_instance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类safe.impact_functions.registry.Registry
的用法示例。
在下文中一共展示了Registry.get_instance方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_impact_function_instance
# 需要导入模块: from safe.impact_functions.registry import Registry [as 别名]
# 或者: from safe.impact_functions.registry.Registry import get_instance [as 别名]
def test_get_impact_function_instance(self):
"""TestRegistry: Test we can get an impact function instance."""
# Getting an IF instance using its class name
registry = Registry()
class_name = 'FloodPolygonBuildingFunction'
impact_function = registry.get_instance(class_name)
result = impact_function.__class__.__name__
message = 'Expecting FloodPolygonBuildingFunction. Got %s ' \
'instead.' % result
self.assertEqual(class_name, result, message)
示例2: test_register_and_clear
# 需要导入模块: from safe.impact_functions.registry import Registry [as 别名]
# 或者: from safe.impact_functions.registry.Registry import get_instance [as 别名]
def test_register_and_clear(self):
"""TestRegistry: Test register and clear impact function."""
registry = Registry()
registry.clear()
message = 'Expecting registry should be cleared. %s impact ' \
'functions exists instead' % len(registry.impact_functions)
self.assertEqual(0, len(registry.impact_functions), message)
registry.register(FloodPolygonBuildingFunction)
message = 'Expecting registry will contains 1 impact functions. %s ' \
'impact functions exists' % len(registry.impact_functions)
self.assertEqual(1, len(registry.impact_functions), message)
result = registry.get_instance('FloodPolygonBuildingFunction')\
.metadata().as_dict()['id']
expected = 'FloodPolygonBuildingFunction'
message = 'Expected registered impact function ID should be %s. ' \
'Got %s instead' % (expected, result)
self.assertEqual(expected, result, message)
示例3: ImpactFunctionManager
# 需要导入模块: from safe.impact_functions.registry import Registry [as 别名]
# 或者: from safe.impact_functions.registry.Registry import get_instance [as 别名]
class ImpactFunctionManager(object):
"""Class for managing metadata for all impact function.
.. versionadded:: 2.1
"""
def __init__(self):
"""Constructor."""
# Singleton Registry to track all the registered Impact Functions
self.registry = Registry()
@property
def impact_functions(self):
"""Return all registered impact functions."""
return self.registry.impact_functions
def get_instance(self, class_name):
"""Return an instance of an impact function given its class name.
.. example::
if_manager = ImpactFunctionManager()
if_class_name = 'FloodBuildingImpactFunction'
if = if_manager.get_instance(if_class_name)
:param class_name: The name of IF class.
:type class_name: str
:return: Impact function instance that matches the argument.
:rtype: safe.impact_functions.base.ImpactFunction
"""
return self.registry.get_instance(class_name)
def get_class(self, class_name):
"""Return the class of an impact function given its class name.
.. example::
if_manager = ImpactFunctionManager()
if_class_name = 'FloodBuildingImpactFunction'
if_class = if_manager.get_class(if_class_name)
:param class_name: the name of IF class
:type class_name: str
:return: impact function class that matches the argument.
:rtype: safe.impact_functions.base.ImpactFunction
"""
return self.registry.get_class(class_name)
def get(self, impact_function_id):
"""Return an instance of an impact function given its ID.
This is a preferred way to get an instance of IF. IF should have a
unique human readable ID in their metadata.
.. example::
if_manager = ImpactFunctionManager()
if_id = 'FloodBuildingImpactFunction'
if = if_manager.get(if_id)
:param impact_function_id: The ID of impact function in the metadata.
:type impact_function_id: str
:return An Impact function instance that has matched id.
:rtype: safe.impact_functions.base.ImpactFunction
"""
impact_functions = self.registry.filter_by_metadata("id", impact_function_id)
if len(impact_functions) == 0:
raise Exception("Impact function with ID: %s not found" % impact_function_id)
elif len(impact_functions) > 1:
raise Exception("There are some Impact Functions that have the same ID: %s" % impact_function_id)
return impact_functions[0].instance()
def filter(self, hazard_metadata=None, exposure_metadata=None):
"""Get available impact functions from hazard and exposure metadata.
Disabled impact function will not be loaded.
.. example::
if_manager = ImpactFunctionManager()
hazard_metadata = {
'subcategory': hazard_flood,
'units': unit_wetdry,
'layer_constraints': layer_vector_polygon
}
exposure_metadata = {
'subcategory': exposure_structure,
'units': unit_building_type_type,
'layer_constraints': layer_vector_polygon
}
ifs = if_manager.filter(hazard_metadata, exposure_metadata)
:param hazard_metadata: The metadata of the hazard.
:type hazard_metadata: dict
:param exposure_metadata: The metadata of the exposure.
:type exposure_metadata: dict
#.........这里部分代码省略.........
示例4: ImpactFunctionManager
# 需要导入模块: from safe.impact_functions.registry import Registry [as 别名]
# 或者: from safe.impact_functions.registry.Registry import get_instance [as 别名]
class ImpactFunctionManager(object):
"""Class for managing metadata for all impact function.
.. versionadded:: 2.1
"""
def __init__(self):
"""Constructor."""
# Singleton Registry to track all the registered Impact Functions
self.registry = Registry()
@property
def impact_functions(self):
"""Return all registered impact functions."""
return self.registry.impact_functions
def get_instance(self, class_name):
"""Return an instance of an impact function given its class name.
.. example::
if_manager = ImpactFunctionManager()
if_class_name = 'FloodBuildingImpactFunction'
if = if_manager.get_instance(if_class_name)
:param class_name: The name of IF class.
:type class_name: str
:return: Impact function instance that matches the argument.
:rtype: safe.impact_functions.base.ImpactFunction
"""
return self.registry.get_instance(class_name)
def get_class(self, class_name):
"""Return the class of an impact function given its class name.
.. example::
if_manager = ImpactFunctionManager()
if_class_name = 'FloodBuildingImpactFunction'
if_class = if_manager.get_class(if_class_name)
:param class_name: the name of IF class
:type class_name: str
:return: impact function class that matches the argument.
:rtype: safe.impact_functions.base.ImpactFunction
"""
return self.registry.get_class(class_name)
def get(self, impact_function_id):
"""Return an instance of an impact function given its ID.
This is a preferred way to get an instance of IF. IF should have a
unique human readable ID in their metadata.
.. example::
if_manager = ImpactFunctionManager()
if_id = 'FloodBuildingImpactFunction'
if = if_manager.get(if_id)
:param impact_function_id: The ID of impact function in the metadata.
:type impact_function_id: str
:return An Impact function instance that has matched id.
:rtype: safe.impact_functions.base.ImpactFunction
"""
impact_functions = self.registry.filter_by_metadata(
'id', impact_function_id)
if len(impact_functions) == 0:
raise Exception(
'Impact function with ID: %s not found' % impact_function_id)
elif len(impact_functions) > 1:
raise Exception(
'There are some Impact Functions that have the same ID: %s' %
impact_function_id)
return impact_functions[0].instance()
def filter(self, hazard_metadata=None, exposure_metadata=None):
"""Get available impact functions from hazard and exposure metadata.
Disabled impact function will not be loaded.
.. example::
if_manager = ImpactFunctionManager()
hazard_metadata = {
'subcategory': hazard_flood,
'units': unit_wetdry,
'layer_constraints': layer_vector_polygon
}
exposure_metadata = {
'subcategory': exposure_structure,
'units': unit_building_type_type,
'layer_constraints': layer_vector_polygon
}
ifs = if_manager.filter(hazard_metadata, exposure_metadata)
:param hazard_metadata: The metadata of the hazard.
#.........这里部分代码省略.........