I had a written code in Python, which works with a database and selects information. Every function in my class creates a session and executes some queries (their quantity varies). Also, each such fragments are placed in try
/except
/finally
.
How can I refactor this to more readable code?
Simple part of my code:
def get_all_user_files(self, data):
try:
log.msg('[GETF] Getting all files for User=%s' % (data['user']))
files_lst = []
session = self.__create_session()
fs_name = str(data['user'] + "_fs")
fs_db = session.execute(sqlalchemy.select([FileSpace]).where(FileSpace.storage_name == fs_name)).fetchone()
catalog = session.execute(sqlalchemy.select([Catalog]).where(Catalog.fs_id == fs_db.id)).fetchone()
files = session.query(FileTable).filter_by(catalog_id=catalog.id)
for record in files.yield_per(1):
files_lst.append((record.original_name, record.user_path))
data['cmd'] = 'READ'
data['files_read'] = files_lst
log.msg('[GETF] GETF data for User=%s has complete!' % (data['user']))
except sqlalchemy.exc.ArgumentError:
log.msg('SQLAlchemy ERROR: Invalid or conflicting function argument is supplied')
except sqlalchemy.exc.CompileError:
log.msg('SQLAlchemy ERROR: Error occurs during SQL compilation')
finally:
session.close()
return data