本文整理汇总了Python中obspy.core.event.ResourceIdentifier.get_referred_object方法的典型用法代码示例。如果您正苦于以下问题:Python ResourceIdentifier.get_referred_object方法的具体用法?Python ResourceIdentifier.get_referred_object怎么用?Python ResourceIdentifier.get_referred_object使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类obspy.core.event.ResourceIdentifier
的用法示例。
在下文中一共展示了ResourceIdentifier.get_referred_object方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_automatic_dereferring_if_resource_id_goes_out_of_scope
# 需要导入模块: from obspy.core.event import ResourceIdentifier [as 别名]
# 或者: from obspy.core.event.ResourceIdentifier import get_referred_object [as 别名]
def test_automatic_dereferring_if_resource_id_goes_out_of_scope(self):
"""
Tests that objects that have no more referrer are no longer stored in
the reference dictionary.
"""
t1 = UTCDateTime(2010, 1, 1) # test object
r_dict = ResourceIdentifier._ResourceIdentifier__resource_id_weak_dict
rid = 'a' # test resource id
# Create object and assert the reference has been created.
r1 = ResourceIdentifier(rid, referred_object=t1)
self.assertEqual(r1.get_referred_object(), t1)
self.assertTrue(rid in r_dict)
# Deleting the object should remove the reference.
del r1
self.assertFalse(rid in r_dict)
# Now create two equal references.
r1 = ResourceIdentifier(rid, referred_object=t1)
r2 = ResourceIdentifier(rid, referred_object=t1)
self.assertEqual(r1.get_referred_object(), t1)
# Deleting one should not remove the reference.
del r1
self.assertEqual(r2.get_referred_object(), t1)
self.assertTrue(rid in r_dict)
# Deleting the second one should
del r2
self.assertFalse(rid in r_dict)
示例2: test_same_resource_id_different_referred_object
# 需要导入模块: from obspy.core.event import ResourceIdentifier [as 别名]
# 或者: from obspy.core.event.ResourceIdentifier import get_referred_object [as 别名]
def test_same_resource_id_different_referred_object(self):
"""
Tests the handling of the case that different ResourceIdentifier
instances are created that have the same resource id but different
objects. This should not happen and thus a warning should be emitted.
"""
object_a = UTCDateTime(1000)
object_b = UTCDateTime(1001)
self.assertEqual(object_a is object_b, False)
id = 'obspy.org/tests/test_resource'
res_a = ResourceIdentifier(id=id,
referred_object=object_a)
# Now create a new resource with the same id but a different object.
# This will raise a warning.
with warnings.catch_warnings(record=True):
warnings.simplefilter('error', UserWarning)
self.assertRaises(UserWarning, ResourceIdentifier,
id=id,
referred_object=object_b)
# Now ignore the warning and actually create the new
# ResourceIdentifier.
warnings.simplefilter('ignore', UserWarning)
res_b = ResourceIdentifier(id=id,
referred_object=object_b)
# Object b was the last to added, thus all resource identifiers will
# now point to it.
self.assertEqual(object_b is res_a.get_referred_object(), True)
self.assertEqual(object_b is res_b.get_referred_object(), True)
示例3: test_same_resource_id_different_referred_object
# 需要导入模块: from obspy.core.event import ResourceIdentifier [as 别名]
# 或者: from obspy.core.event.ResourceIdentifier import get_referred_object [as 别名]
def test_same_resource_id_different_referred_object(self):
"""
Tests the handling of the case that different ResourceIdentifier
instances are created that have the same resource id but different
objects. The referred objects should still return the same objects
used in the ResourceIdentifier construction or set_referred_object
call. However, if an object is set to a resource_id that is not
equal to the last object set it should issue a warning.
"""
warnings.simplefilter('default')
object_a = UTCDateTime(1000)
object_b = UTCDateTime(1000)
object_c = UTCDateTime(1001)
self.assertFalse(object_a is object_b)
id = 'obspy.org/tests/test_resource'
res_a = ResourceIdentifier(id=id, referred_object=object_a)
# Now create a new resource with the same id but a different object.
# This should not raise a warning as the object a and b are equal.
with warnings.catch_warnings(record=True) as w:
res_b = ResourceIdentifier(id=id, referred_object=object_b)
self.assertEqual(len(w), 0)
# if the set object is not equal to the last object set to the same
# resource_id, however, a warning should be issued.
with warnings.catch_warnings(record=True) as w:
res_c = ResourceIdentifier(id=id, referred_object=object_c)
self.assertEqual(len(w), 1)
expected_text = 'which is not equal to the last object bound'
self.assertIn(expected_text, str(w[0]))
# even though the resource_id are the same, the referred objects
# should point to the original (different) objects
self.assertIs(object_a, res_a.get_referred_object())
self.assertIs(object_b, res_b.get_referred_object())
self.assertIs(object_c, res_c.get_referred_object())
示例4: test_latest_in_scope_object_returned
# 需要导入模块: from obspy.core.event import ResourceIdentifier [as 别名]
# 或者: from obspy.core.event.ResourceIdentifier import get_referred_object [as 别名]
def test_latest_in_scope_object_returned(self):
"""
Test that the most recently defined object with the same resource_id,
that is still in scope, is returned from the get_referred_object
method
"""
cat1 = read_events()
# The resource_id attached to the first event is self-pointing
self.assertIs(cat1[0], cat1[0].resource_id.get_referred_object())
# make a copy and re-read catalog
cat2 = cat1.copy()
cat3 = read_events()
# the resource_id on the new catalogs point to their attached objects
self.assertIs(cat1[0], cat1[0].resource_id.get_referred_object())
self.assertIs(cat2[0], cat2[0].resource_id.get_referred_object())
self.assertIs(cat3[0], cat3[0].resource_id.get_referred_object())
# now delete cat1 and make sure cat2 and cat3 still work
del cat1
self.assertIs(cat2[0], cat2[0].resource_id.get_referred_object())
self.assertIs(cat3[0], cat3[0].resource_id.get_referred_object())
# create a resource_id with the same id as the last defined object
# with the same resource id (that is still in scope) is returned
new_id = cat2[0].resource_id.id
rid = ResourceIdentifier(new_id)
self.assertIs(rid.get_referred_object(), cat3[0])
del cat3
self.assertIs(rid.get_referred_object(), cat2[0])
del cat2
self.assertIs(rid.get_referred_object(), None)
示例5: test_resources_in_global_dict_get_garbage_collected
# 需要导入模块: from obspy.core.event import ResourceIdentifier [as 别名]
# 或者: from obspy.core.event.ResourceIdentifier import get_referred_object [as 别名]
def test_resources_in_global_dict_get_garbage_collected(self):
"""
Tests that the ResourceIdentifiers in the class level resource dict get
deleted if they have no other reference and the object they refer to
goes out of scope.
"""
obj_a = UTCDateTime()
obj_b = UTCDateTime()
res1 = ResourceIdentifier(referred_object=obj_a)
res2 = ResourceIdentifier(referred_object=obj_b)
# Now two keys should be in the global dict.
rdict = ResourceIdentifier._ResourceIdentifier__resource_id_weak_dict
self.assertEqual(len(list(rdict.keys())), 2)
del obj_a, obj_b
self.assertIs(res1.get_referred_object(), None)
self.assertIs(res2.get_referred_object(), None)
示例6: test_resources_in_global_dict_get_garbage_collected
# 需要导入模块: from obspy.core.event import ResourceIdentifier [as 别名]
# 或者: from obspy.core.event.ResourceIdentifier import get_referred_object [as 别名]
def test_resources_in_global_dict_get_garbage_collected(self):
"""
Tests that the ResourceIdentifiers in the class level resource dict get
deleted if they have no other reference and the object they refer to
goes out of scope.
"""
obj_a = UTCDateTime()
obj_b = UTCDateTime()
res1 = ResourceIdentifier(referred_object=obj_a)
res2 = ResourceIdentifier(referred_object=obj_b)
# Now two keys should be in the global dict.
rdict = ResourceIdentifier._ResourceIdentifier__resource_id_weak_dict
self.assertEqual(len(list(rdict.keys())), 2)
# Deleting the objects should also remove the from the dictionary.
del obj_a, obj_b
self.assertEqual(len(list(rdict.keys())), 0)
# references are still around but no longer have associates objects.
self.assertEqual(res1.get_referred_object(), None)
self.assertEqual(res2.get_referred_object(), None)
示例7: test_getting_gc_no_shared_resource_id
# 需要导入模块: from obspy.core.event import ResourceIdentifier [as 别名]
# 或者: from obspy.core.event.ResourceIdentifier import get_referred_object [as 别名]
def test_getting_gc_no_shared_resource_id(self):
"""
Test that calling get_referred_object on a resource id whose object
has been garbage collected, and whose resource_id is unique,
returns None
"""
obj1 = UTCDateTime()
rid1 = ResourceIdentifier(referred_object=obj1)
# delete obj1, make sure rid1 return None
del obj1
self.assertIs(rid1.get_referred_object(), None)
示例8: test_getting_gc_with_shared_resource_id
# 需要导入模块: from obspy.core.event import ResourceIdentifier [as 别名]
# 或者: from obspy.core.event.ResourceIdentifier import get_referred_object [as 别名]
def test_getting_gc_with_shared_resource_id(self):
"""
Test that calling get_referred_object on a resource id whose object
has been garbage collected, but that has another object that shares
the same resource_id, returns the other object with the same resource
id and issues a warning
"""
uri = 'testuri'
obj1 = UTCDateTime(1000)
obj2 = UTCDateTime(1000)
rid1 = ResourceIdentifier(uri, referred_object=obj1)
rid2 = ResourceIdentifier(uri, referred_object=obj2)
self.assertFalse(rid1.get_referred_object() is
rid2.get_referred_object())
self.assertNotEqual(rid1._object_id, rid2._object_id)
del obj1
warnings.simplefilter('default')
with warnings.catch_warnings(record=True) as w:
rid1.get_referred_object()
self.assertEqual(len(w), 1)
self.assertIn('The object with identity', str(w[0]))
# now both rids should return the same object
self.assertIs(rid1.get_referred_object(), rid2.get_referred_object())
# the object id should now be bound to obj2
self.assertEqual(rid1._object_id, rid2._object_id)
示例9: test_getting_gc_no_shared_resource_id
# 需要导入模块: from obspy.core.event import ResourceIdentifier [as 别名]
# 或者: from obspy.core.event.ResourceIdentifier import get_referred_object [as 别名]
def test_getting_gc_no_shared_resource_id(self):
"""
Test that calling get_referred_object on a resource id whose object
has been garbage collected, and whose resource_id is unique,
returns None
"""
obj1 = UTCDateTime()
rid1 = ResourceIdentifier(referred_object=obj1)
# delete obj1, make sure rid1 return None
del obj1
# raises UserWarning
with warnings.catch_warnings():
warnings.simplefilter("ignore", UserWarning)
self.assertIs(rid1.get_referred_object(), None)
示例10: test_resource_ids_refer_to_newest_object
# 需要导入模块: from obspy.core.event import ResourceIdentifier [as 别名]
# 或者: from obspy.core.event.ResourceIdentifier import get_referred_object [as 别名]
def test_resource_ids_refer_to_newest_object(self):
"""
Tests that resource ids which are assigned multiple times but point to
identical objects always point to the newest object. This prevents some
odd behaviour.
"""
t1 = UTCDateTime(2010, 1, 1)
t2 = UTCDateTime(2010, 1, 1)
rid = ResourceIdentifier("a", referred_object=t1)
rid = ResourceIdentifier("a", referred_object=t2)
del t1
self.assertEqual(rid.get_referred_object(), t2)
示例11: test_id_without_reference_not_in_global_list
# 需要导入模块: from obspy.core.event import ResourceIdentifier [as 别名]
# 或者: from obspy.core.event.ResourceIdentifier import get_referred_object [as 别名]
def test_id_without_reference_not_in_global_list(self):
"""
This tests some internal workings of the ResourceIdentifier class.
NEVER modify the __resource_id_weak_dict!
Only those ResourceIdentifiers that have a reference to an object that
is referred to somewhere else should stay in the dictionary.
"""
r_dict = ResourceIdentifier._ResourceIdentifier__resource_id_weak_dict
_r1 = ResourceIdentifier() # NOQA
self.assertEqual(len(list(r_dict.keys())), 0)
# Adding a ResourceIdentifier with an object that does not have a
# reference will result in a dict that contains None, but that will
# get removed when the resource_id goes out of scope
_r2 = ResourceIdentifier(referred_object=UTCDateTime()) # NOQA
self.assertEqual(_r2.get_referred_object(), None)
del _r2 # delete rid to get its id out of r_dict keys
# Give it a reference and it will stick around.
obj = UTCDateTime()
_r3 = ResourceIdentifier(referred_object=obj) # NOQA
self.assertEqual(len(list(r_dict.keys())), 1)
示例12: test_adding_a_referred_object_after_creation
# 需要导入模块: from obspy.core.event import ResourceIdentifier [as 别名]
# 或者: from obspy.core.event.ResourceIdentifier import get_referred_object [as 别名]
def test_adding_a_referred_object_after_creation(self):
"""
Check that the referred objects can also be made available after the
ResourceIdentifier instances have been created.
"""
obj = UTCDateTime()
res_id = "obspy.org/time/test"
ref_a = ResourceIdentifier(res_id)
ref_b = ResourceIdentifier(res_id)
ref_c = ResourceIdentifier(res_id)
# All three will have no resource attached.
self.assertEqual(ref_a.get_referred_object(), None)
self.assertEqual(ref_b.get_referred_object(), None)
self.assertEqual(ref_c.get_referred_object(), None)
# Setting the object for one will make it available to all other
# instances, provided they weren't bound to specific objects.
ref_b.set_referred_object(obj)
self.assertIs(ref_a.get_referred_object(), obj)
self.assertIs(ref_b.get_referred_object(), obj)
self.assertIs(ref_c.get_referred_object(), obj)