How to get extern data for my system exception subroutine?

I am still not able to get a extern class set for my special system exception subroutine. See my example:

File ‘web.py’ contains
#-------------------------------- Import modules -------------------------------
import importlib
import os
import sys
#-------------------------------- Initialize data ------------------------------
msg = ‘’
syspth = ‘bsx’
#*------------------------------- Once-only processing -------------------------
while(True):
#------------------------------ Import function ‘webbs1’ ---------------------
flifnc = syspth + ‘/pgm/webbs1.py’
if not os.path.isfile(flifnc):
msg = ‘Missing function file ‘’ + flifnc + ‘’’
break
z = importlib.import_module(flifnc[0:-3].replace(’/’, ‘.’))
#------------------------------ Call function ‘webbs1’ -----------------------
msg, glb = z.webbs1(syspth)
#-----------------------------------------------------------------------------
print('Log information from -web-: ’ + glb.flplog)
break
#-------------------------------- Print message --------------------------------
if msg != ‘’:
print(msg)
#-------------------------------------------------------------------------------

File ‘webbs1.py’ contains
#=== File ‘webbs1.py’ =========== Process web base system (part 1/2) ===========
def webbs1(syspth):
#------------------------------ Import modules -------------------------------
import importlib
import os
import sys
import time
#------------------------------ Initialize data ------------------------------
glb = False
msg = ‘’
#------------------------------ Once-only processing -------------------------
swtrun = True
while(swtrun):
#---------------------------- Import web global data -----------------------
flicls = ‘bsx/pgm/cls/webglb.py’
if not os.path.isfile(flicls):
msg = ‘Missing web global data class file ‘’ + flicls + ‘’’
break
z = importlib.import_module(flicls[0:-3].replace(’/’, ‘.’))
glb = z.WebGlb()
glb.syspth = syspth
#---------------------------- Initialize log data --------------------------
flilog = glb.syspth + glb._FLILOG
indwrk = flilog.rfind(’/’)
if not os.path.exists(flilog[:indwrk]):
os.makedirs(flilog[:indwrk])
if not os.path.isfile(flilog):
open(flilog, ‘w’, encoding=‘utf-8’).close()
glb.flolog = open(flilog, ‘a’, encoding=‘utf-8’)
glb.flplog = flilog
#---------------------------- Initialize system exception handler ----------
sys.excepthook = sysexc
xxx = xxx # WRONG statement going to function ‘sysexc’
msg = ‘Successfully executed!’
swtrun = False
#---------------------------------------------------------------------------
#-----------------------------------------------------------------------------
return (msg, glb)

#-------------------------------------------------------------------------------
def sysexc( # System exception subroutine ------------------
exctpx, # …
excmsg, # …
trb # …
):
#------------------------------ Import modules -------------------------------
import traceback
#------------------------------ Create log message ---------------------------
print(‘Starting exception routine …’)
print(’-1-’)
print(exctpx)
print(’-2-’)
print(excmsg)
for pstwrk in range(len(traceback.format_tb(trb))-1 , -1, -1):
print(’-3-’)
print(traceback.format_tb(trb)[pstwrk].replace(’\n ‘, ‘:’)[1:])
print(’-4’)
print(‘Log information from -web-: … HOW to get data from global data?’)
#===============================================================================

File 'WebGlb.py’
# File ‘WebGlb.py’ ============== Web global data ==============================
class WebGlb:
#-----------------------------------------------------------------------------
def init(self):
#---------------------------- Initialize file names-------------------------
self._FLILOG = ‘/log/log.txt’
self._FLIFNC = ‘/pyx/.py’
self._FLIPKL = '/txt/
.pkl’
self._FLISBR = ‘/pyx/sbr/***.py’
#---------------------------- Initialize attributes ------------------------
self.flolog = False # Web log file object
self.flplog = False # Web log file path
self.glb = False # Web global data object
# (needed for function sysexc)
self.tblsbr = { # Table of subroutines
‘websbrlog’ : ‘’,
‘websbrtxt’ : ‘’
}
#---------------------------------------------------------------------------
#-----------------------------------------------------------------------------
#===============================================================================

I am very much looking forward to your satisfactory solution!

Your question is not very clear. First, format your code as described in the about post. That makes it formatted so it will be much clearer.

I created a small example, a file called importables.py in the current working directory that contains:


    class UnexpectedText():
        quote = "Nobody expects the Spanish inquisition"

    unexpected_instance = UnexpectedText()

First we get the instance from importables:

>>> from importables import unexpected_instance
>>> print(unexpected_instance.quote)
Nobody expects the Spanish inquisition

We can also get the whole namespace as names, you know the syntax:

>>> import importables
>>> print(importables.unexpected_instance.quote)
Nobody expects the Spanish inquisition

How does this not answer your question?

Now I ask you how your function ‘UnexpectedText’ can get the required external variable like ‘glb’. In my program it contains the web global data. Namely, I want to get the web log global object in such a way that I can write the log information to that log file. Hope it’s clear enough for you.

Don’t turn my example upside down! My UnexpectedText plays the role of your WebGlb. Let me rephrase my question, maybe that will make it clearer: What is so special about the WebGlb module that you think you cannot import it? If you can import it, you can instantiate the WebGlb class and use the object so created to fill your log.

Some remarks:

  1. Why do you put all the items in the class WebGlb? They will not change and are in no way related to each other. Fields at the module level will suffice.
  2. Global data is evil. Why do you not keep the items with the classes they belong to? E.g. the logging directory with the code producing logs?
  3. Related: The standard library has a logging module which contains a lot of goodies for logging. Use that and do not reinvent the wheel.