0

Problem:

I am trying to call a static method from a class method which throws an error. What is the right way to call that method in Python 3.x?

NameError: name 'strip_sensitive_data' is not defined 

Code:

import sentry_sdk from scrapy.exceptions import NotConfigured class SentryLogging(object): """ Send exceptions and errors to Sentry. """ def strip_sensitive_data(event, hint): if event['level'] == 'error': return event else: return None @classmethod def from_crawler(cls, crawler): sentry_dsn = crawler.settings.get('SENTRY_DSN', None) environment = crawler.settings.get('ENVIRONMENT', None) if sentry_dsn is None: raise NotConfigured # instantiate the extension object ext = cls() # instantiate sentry_sdk.init( sentry_dsn, traces_sample_rate=1.0, environment= environment, before_send=strip_sensitive_data ) # return the extension object return ext 
5
  • 3
    Move it out of your class, or use SentryLogging.strip_sensitive_data. Consider also @staticmethod. Commented Dec 16, 2020 at 18:04
  • This post may be helpful. It sounds very similar to your question. Commented Dec 16, 2020 at 18:06
  • 1
    Maybe you meant cls.strip_sensitive_data? Commented Dec 16, 2020 at 18:06
  • 1
    (1) Add the @staticmethod decorator to strip_sensitive_data (2) You can call it from the class, e.g. cls.strip_sensitive_data(...), or from outside of the class method you can do SentryLogging.strip_sensitive_data(...), but add the missing decorator first. Commented Dec 16, 2020 at 18:08
  • Thank you all! That seemed to have done the trick. Commented Dec 16, 2020 at 18:11

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.