[obspy-users] subclass of obspy.core.event.magnitude.Magnitude object (or other event object)

Lion Krischer lion.krischer at erdw.ethz.ch
Wed Mar 6 21:20:39 CET 2019

Hey Christian,

most event classes are autogenerated and contain some "magic" to make
handling them easier. One of these things is to warn on setting unknown
attributes (this would otherwise a great source of unspotted errors due
to typos).

There are two ways around this - either turn off warning with
non-default keys or add it as a default key. Also best inherit from
Magnitude instead of _Magnitude.

from obspy.core.event import Magnitude

class Magnitude_User(Magnitude):
    # Either do this.
    warn_on_non_default_key = False
    def __init__(self, fc, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # Or add it as a default value.
        self.defaults['fc'] = None
        self.fc = fc



On 05.03.19 00:02, Christian BAILLARD wrote:
> Dear obspy users,
> I'm working on some magnitude estimates and I would like do create a
> subclass inheriting from "magnitude.Magnitude" obspy object. At this
> point I just want to have the same attributes as the parent class plus
> some very simple new attributes such as "corner frequency", "omega_0"...
> I just did the following:
> class Magnitude_User(_Magnitude):
>     def __init__(self,*args, **kwargs):
>         # initialization code
>         self.fc=kwargs.get('fc')
>         with warnings.catch_warnings():
>             warnings.simplefilter("ignore")
>             super().__init__(*args, **kwargs)
> I wanted to get rid of the warning, but I still get the following:
> UserWarning: Setting attribute "fc" which is not a default attribute
> What would you advise for defining a new attribute for an obspy class
> without directly modifying the parent class? Is that the best
> approach? If yes, how can I get rid of the "warnings.warn"?
> I thought about using the AttribDict() and the "extra" attribute but
> I'm not really interested in writing any xml for now and I would
> prefer a more direct approach.
> Thanks for your help,
> Christian
> _______________________________________________
> obspy-users mailing list
> obspy-users at lists.swapbytes.de
> https://lists.swapbytes.de/mailman/listinfo/obspy-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.swapbytes.de/pipermail/obspy-users/attachments/20190306/b533648c/attachment.html>

More information about the obspy-users mailing list