Пример #1
0
    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
0
 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())
Пример #3
0
 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)
Пример #4
0
    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)
Пример #5
0
 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)
Пример #6
0
 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())
Пример #7
0
 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)
Пример #8
0
 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
     # raises UserWarning
     with warnings.catch_warnings():
         warnings.simplefilter("ignore", UserWarning)
         self.assertIs(rid.get_referred_object(), cat2[0])
     del cat2
     self.assertIs(rid.get_referred_object(), None)
Пример #9
0
 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)
Пример #10
0
 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)
Пример #11
0
 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)
Пример #12
0
 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)
Пример #13
0
 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)
Пример #14
0
 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)
Пример #15
0
 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)
Пример #16
0
 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)
Пример #17
0
 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)
Пример #18
0
 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)
Пример #19
0
    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)  # @UnusedVariable
        rid = ResourceIdentifier("a", referred_object=t2)

        del t1

        self.assertEqual(rid.get_referred_object(), t2)
Пример #20
0
    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)
Пример #21
0
    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)
Пример #22
0
    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)
Пример #23
0
 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)
Пример #24
0
 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)
Пример #25
0
from obspy.core.event import ResourceIdentifier
from obsplus.events.validate import validate_catalog

if __name__ == "__main__":
    cat_path = Path("/media/data/Gits/obsplus/tests/test_data/qml2merge/"
                    "2016-10-15T02-27-50/2016-10-15T02-27-50_2.xml")
    assert cat_path.exists()

    cat = obspy.read_events(str(cat_path))

    # remove all amplitudes
    picks = cat[0].picks
    cat[0].picks = [x for x in picks if not x.phase_hint == "IAML"]

    # get duplicated event ids
    pdf = obsplus.picks_to_df(cat)
    pdf = pdf[pdf["evaluation_status"] != "rejected"]
    duplicated_ids = set(pdf[pdf["station"].duplicated()]["resource_id"])

    # next mark duplicated as rejected and prune the event
    for duplicated_id in duplicated_ids:
        rid = ResourceIdentifier(duplicated_id)
        obj = rid.get_referred_object()
        obj.evaluation_status = "rejected"

    cat_out = obsplus.events.utils.prune_events(cat)

    validate_catalog(cat_out)

    cat.write(str(cat_path), "quakeml")