Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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
share|improve this question

closed as off-topic by 200_success Jul 28 '14 at 21:27

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. Such questions may be suitable for Stack Overflow or Programmers. After the question has been edited to contain working code, we will consider reopening it." – 200_success
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Please check your indentation. –  200_success Jul 28 '14 at 21:27