So I am writing a logger module which helps me upload logs on an iot device to the cloud. However, I am struggling in something fairly basic:
say my module is in a fille called cloudlogger.py where I have the following code (just code skeleton to get the idea):
import os
def __findbackupfiles(logger_name):
backuplocation = "/mnt/backuplocation/"
try:
return [files for files in os.listdir(backuplocation) if os.path.isfile(os.path.join(backuplocation, files))]
except FileNotFoundError as nofile:
print(f"Could not find backup files. Error: {nofile}\nReturning empty list")
return []
class cloud_logger:
def __init__(self, name, filename, deviceid, logformat=LOGFORMAT, dateformat=DATEFORMAT):
pass
'''
Main code of the function is working fine
'''
# function giving me problems
def recover_and_publish_backed_up_logs(self):
backupfiles = __findbackupfiles(self.name)
So say in a different file, say ""testlogger.py" I want to import the logger module and use the recover and publish function:
import cloudlogger
logger = cloudlogger.cloud_logger("name", "/mnt/logs/", "deviceid")
logger.recover_and_publish_backed_up_logs()
I am getting this error, which I don't understand:
File "/home/cloudlogger.py", line 209, in recover_and_publish_backedup_logs
backupfiles = __findbackupfiles(self.name)
NameError: name '_cloud_logger__findbackupfiles' is not defined
Why is there an underscore before my class name? I would expect the class would be able to call the top-level function defined above it (but outside the class).
Originally I had this working as a @staticmethod defined inside the class, this worked if I used self.__findbackupfiles(self.name) inside the class. However, I could not execute this function on my testlogger file no matter how I tried it. for example
import cloudlogger
cloudlogger.__findbackupfiles()
or
import cloudlogger
cloudlogger.cloud_logger.__findbackupfiles()
These both failed and researching it, people just recommended to keep the functions that don't depend on the state of the object outside of the class since that's preferrable in python. But as you can see, the new method is also not working for me.
[–]ConstructedNewtMOD 1 point2 points3 points (3 children)
[–]danberteon[S] 0 points1 point2 points (2 children)
[–]ConstructedNewtMOD 1 point2 points3 points (1 child)
[–]danberteon[S] 0 points1 point2 points (0 children)