python - Sharing external module/function between own classes -


question sharing 'functions' between classes.
situation:

  • all own code in 1 file
  • i'm using python-daemon daemonize script
  • that uses class (doorcamdaemon) initiate , run.
  • it imports class (doorcam) has looping function
  • i'm using sample script daemon functions, , shows how use logging module.

the logging works main part of script , in doorcamdaemon class, not in doorcam class.

class doorcamdaemon():     def __init__(self):         #skipping content, not related issue         self.doorcam=doorcam()      def run(self):         self.doorcam.startlistening() #looping function  class doorcam     def __init__(self):         #skipping somecontent, not related issue     def startlistening(self):         while true:             logger.info('hello')  app = doorcamdaemon() logger = logging.getlogger("doorcamlog") logger.setlevel(logging.debug) formatter = logging.formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") handler = logging.filehandler("/var/log/doorcam.log") handler.setformatter(formatter) logger.addhandler(handler)  daemon_runner = runner.daemonrunner(app) daemon_runner.daemon_context.files_preserve=[handler.stream] daemon_runner.do_action() 

the error returned is:

$ ./doorcam.py start traceback (most recent call last):   file "./doorcam.py", line 211, in <module>     app = doorcamdaemon()   file "./doorcam.py", line 159, in __init__     self.doorcam=doorcam()   file "./doorcam.py", line 18, in __init__     logger.info('doorcam started capturing') nameerror: global name 'logger' not defined 

so obvious question: how can make work in doorcam class well?

try moving line

app = doorcamdaemon() 

to after lines create , set logger. traceback telling you:

  1. logger doesn't exist in line 18 doorcam's constructor tries use it

  2. doorcamdaemon tries construct doorcam @ line 159 in own constructor

so can't create doorcamdaemon if logger isn't defined yet.

some of content omitted in doorcam.__init__ was related issue :)


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -