Wer direkt zum Punkt will, skippt ambesten zum
ich habe ein kleines Verständnisproblem mit meinem Programm...
Um komplikationen zu vermeiden Poste ich einfach mal den ganzen Code.
Also dieses Programm hat in erster Linie den Zweck Aktionen zu loggen und Exceptions zu verhindern.
Code: Alles auswählen
# -*- coding: cp1252 -*-
#Version 0.2
#Calllback is a little logging module designed by Zuzu_Typ, for private use ONLY!
import os, sys, time, string
import traceback as traceb
usage = """This Module wasn't designed for being run as a standalone file.
Please import it with
from Callback import Callback
or
import Callback
Create a Callback instance with VAR = Callback()
You can then call a function:
VAR.call(FUNCTION,ARGUMENTS,KEYWORD ARGUMENTS)
"""
class Callback:
def __init__(self,filepath=os.getcwd(),filename="CB_LOGGER.LOG",foldername="CB_LOGGER_BACKUP",log=True):
self.log = log
if log:
self.filename = filename
self.filepath = filepath
self.foldername = foldername
self.logfilecontent = ["#LOGFILE 'n'#\n"]
self.logfilecontent.append(";Logfile generiert von Callback v 0.2\n")
timeline = "(vom "+str(time.localtime().tm_mday)+"."+str(time.localtime().tm_mon)+"."+str(time.localtime().tm_year)+" um "+str(time.localtime().tm_hour)+"."+str(time.localtime().tm_min)+"."+str(time.localtime().tm_sec)+" Uhr)"
self.logfilecontent.append(";"+timeline)
self.backupfile = self.filepath+"//"+self.foldername+"//"+"Logfile "+timeline+self.filename[-4:]
try:
os.mkdir(self.foldername)
except WindowsError:
pass
file_ = open(self.backupfile,"w")
file_.close()
try:
file_ = open(self.filepath+"//"+self.filename,"w")
except:
raise IOError("Callback Logfile creation failed! Path or Filename: "+"´"+self.filepath+self.filename+"´")
file_.close()
self.update()
def update(self):
file_ = open(self.filepath+"//"+self.filename,"w")
file_.writelines(self.logfilecontent)
file_.close()
file_ = open(self.backupfile,"w")
file_.writelines(self.logfilecontent)
file_.close()
def get_current_time(self):
return "["+str(time.localtime().tm_mday)+"."+str(time.localtime().tm_mon)+"."+str(time.localtime().tm_year)+"|"+str(time.localtime().tm_hour)+":"+str(time.localtime().tm_min)+"."+str(time.localtime().tm_sec)+"]"
def call(self,function,log=False,*arg,**kw):
if self.log and log:
self.logfilecontent.append("\n")
self.logfilecontent.append(self.get_current_time())
self.logfilecontent.append("Calling "+str(function)+" with ( arg: "+str(arg)+" | kw: "+str(kw)+" )")
self.update()
try:
return function(*arg,**kw)
except:
if self.log and log:
self.logfilecontent.append("\n")
self.logfilecontent.append(self.get_current_time())
self.logfilecontent.append("Exception raised: \n\n")
exc_info = sys.exc_info()
traceback_info = traceb.extract_tb(exc_info[2])
self.logfilecontent.append('Traceback (most recent call last):')
for i in traceback_info:
self.logfilecontent.append("\n")
self.logfilecontent.append(' File "'+i[0]+'", line '+str(i[1])+', in '+i[2]+"\n "+i[3])
string_ = str(exc_info[0])
exception = string.split(string.split(string_,"<type 'exceptions.")[1],"'>")[0]
self.logfilecontent.append("\n"+exception+": "+str(exc_info[1])+"\n")
self.update()
else:
exc_info = sys.exc_info()
traceback_info = traceb.extract_tb(exc_info[2])
print "Exception raised: \n",'Traceback (most recent call last):'
for i in traceback_info:
print(' File "'+i[0]+'", line '+str(i[1])+', in '+i[2]+"\n "+i[3])
string_ = str(exc_info[0])
exception = string.split(string.split(string_,"<type 'exceptions.")[1],"'>")[0]
print(exception+": "+str(exc_info[1]))
if __name__ == "__main__":
def test(a):
print(ab)
CB = Callback(log=False)
CB.call(test,1,2)
Dort wird eine Funktion "test" definiert, die ein normales Argument "a" anfordert und versucht eine nicht definierte Variable auszugeben "print(ab)"
Dann wird eine Instanz von der Klasse Callback erstellt, wobei das logging ausgeschaltet wird.
Danach wird ein Callback aufgerufen und zwar mit der Funktion test und dem Argument 1.
Was nun passieren sollte, ist dass das Programm die folgende Meldung ausgibt (geprintet und nicht als exception):
Code: Alles auswählen
Exception raised:
Traceback (most recent call last):
File "C:\Users\Zuzu_Typ\Desktop\Callback.py", line 64, in call
return function(*arg,**kw)
File "C:\Users\Zuzu_Typ\Desktop\Callback.py", line 93, in test
print ab
NameError: global name 'ab' is not defined
Code: Alles auswählen
Exception raised:
Traceback (most recent call last):
File "C:\Users\Zuzu_Typ\Desktop\Callback.py", line 64, in call
return function(*arg,**kw)
TypeError: test() takes exactly 1 argument (0 given)
Die call Funktion fordert eigentlich nur das Argument function, das die auszuführende Funktion angibt und das keyword argument log, mit dem man explizit für eine bestimmte Funktion das Logging ausschalten kann. Alle anderen (keyword) arguments werden mit den Variablen arg und kw aufgenommen.
wird die Funktion call nun mit (test,1) ausgeführt, wird function die Funktion test zugewiesen und nun kommen wir zu meinem nicht-verständnis:
dem Keyword argument log wird der Wert 1 zugewiesen, was eigentlich nicht passieren sollte.
Ist das eine Normale reaktion?
Vielen Dank für eure Hilfe!
Beste Grüße,
--Zuzu_Typ--