load_file.py
class Connection:
def __init__(self):
self.connection_type = None
self.server = None
self.port = None
self.user = None
self.password = None
self.isActive = None
self.file_extension = None
self.file_contains = None
self.file_location = None
self.schedule_minutes = None
self.interval_time = None
self.last_ran = None
def data_connection(self, data_connection_detail):
self.connection_type = data_connection_detail[1]
self.server = data_connection_detail[2]
self.port = data_connection_detail[3]
self.user = data_connection_detail[4]
self.password = data_connection_detail[5]
self.isActive = data_connection_detail[6]
self.file_extension = data_connection_detail[7]
self.file_contains = data_connection_detail[8]
self.file_location = data_connection_detail[9]
self.schedule_minutes = data_connection_detail[10]
self.interval_time = data_connection_detail[11]
self.last_ran = datetime.now()
def get_connection(self):
cursor = connection.cursor()
cursor.execute(
"SELECT ID, Type, Server, Port, User, Password, isActive, FileExtension, FileContains, FileLocation, "
"ScheduleMinutes, IntervalTime, LastRan from DataConnection WHERE isActive=True")
return cursor.fetchall()
server_ftp.py
class EasyFTP:
def __init__(self):
self.type_email = None
self.isActive = None
self.password = None
self.user = None
self.port = None
self.server = None
self.connection_type = None
self.file_extension = None
self.file_contains = None
self.file_location = None
def easy_ftp(self, data_connection_detail):
# enter FTP server fields into datagrip once active
self.type_email = data_connection_detail[1]
self.server = data_connection_detail[2]
self.port = data_connection_detail[3]
self.user = data_connection_detail[4]
self.password = data_connection_detail[5]
self.isActive = data_connection_detail[6]
self.file_extension = data_connection_detail[7]
self.file_contains = data_connection_detail[8]
self.file_location = data_connection_detail[9]
# using hard coded config for FTP server fields for testing
FTP_HOST = self.server
FTP_USER = self.user
FTP_PASS = self.password
# connect to the FTP server
with FTP(FTP_HOST, FTP_USER, FTP_PASS) as ftp:
ftp.encoding = "utf-8"
ftp.cwd('/files')
for filename in ftp.nlst():
# fnmatch compares single filename against a pattern '*' = matches everything
if fnmatch.fnmatch(filename, f'*{self.file_contains}*{self.file_extension}'):
with open(f'{self.file_location}/{filename}', 'wb') as fp:
ftp.retrbinary(f'RETR {filename}', fp.write)
what I am trying to do is not repeat using the 'data_connection_detail[1], [2], etc inside of the easy_ftp function as it is not a good practice. Instead I am trying to use the objects attributes from the function data_connection inside of the easy_ftp function.
If there is anything I could clarify further please let me know. Thank you!
CodePudding user response:
a better practice would be to inherit the Connection class inside the EasyFTP class and calling the init of the parent class (in this case Connection) then the easyftp class will automatically have all attributes already set
example EasyFTP:
import Connection
class EasyFTP(Connection):
def __init__(self):
super().__init__()
print(self.server)
CodePudding user response:
Partial solution; Not an answer and tangential to the question, but might something like this be a little bit tidier, and make future modifications much easier?
You've currently got the following code
def data_connection(self, data_connection_detail):
self.connection_type = data_connection_detail[1]
self.server = data_connection_detail[2]
self.port = data_connection_detail[3]
self.user = data_connection_detail[4]
self.password = data_connection_detail[5]
self.isActive = data_connection_detail[6]
self.file_extension = data_connection_detail[7]
self.file_contains = data_connection_detail[8]
self.file_location = data_connection_detail[9]
self.schedule_minutes = data_connection_detail[10]
self.interval_time = data_connection_detail[11]
self.last_ran = datetime.now()
This could be simplified into the much shorter
def data_connection(self,data_connection_detail):
connection_type,server,port,user,password,isActive,file_extension,file_contains,file_location,schedule_minutes,interval_time,last_ran=*data_connection_detail[1:],datetime.now()
self.__dict__.update(locals())
Alternatively, maybe it's easier to pass data_connection_detail
as a dictionary? Then you could just do
def data_connection(self, data_connection):
self.__dict__.update(data_connection|{'last_ran':datetime.now()})