Python Forum
Whats a good design/approach?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Whats a good design/approach?
#1
Hi all.

I have this simple code, which does pretty simple things as open a file within the class. But i cant decide what the best design. Is approach1 is better approach2? I need to understand when re-raise an exception and also is it better to catch FileNotFoundError in the class or in the main. What the best approach?

import json
import sys

class Approach2FileNotFoundException(Exception):
    pass


class Approach1:

    def __init__(self, config):
        self.config = self._open(config)

    def _open(self, config):
        with open(config, "r") as fh:
            return json.load(fh)


class Approach2:

    def __init__(self, config):
        self.config = self._open(config)

    def _open(self, config):
        try:
            with open(config, "r") as fh:
                return json.load(fh)
        except FileNotFoundError as err:
            raise Approach2FileNotFoundException(err)


def main():
    try:
        a1 = Approach1("./config.txt")
    except FileNotFoundError as err:
        print(err)
        sys.exit(1)

    try:
        a2 = Approach2("./config.txt")
    except Approach2FileNotFoundException as err:
        print(err)
        sys.exit(1)


if __name__ == "__main__":
    main()
Many thanks
Reply
#2
(Sep-15-2019, 10:11 PM)hshivaraj Wrote: Is approach1 is better approach 2
Both work in this case,approach 2 you can modify the error message.
It's not unclear what happen here in the case without try,except.
def _open(config):
    with open(config, "r") as fh:
        return fh.read()
>>> _open('foo.txt')
'hello'

>>> _open('foo999.txt')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "file_try.py", line 3, in _open
    with open(config, "r") as fh:
FileNotFoundError: [Errno 2] No such file or directory: 'foo999.txt'

[Errno 2] No such file or directory: 'foo999.txt'
Shorter just print the error from last line over.
def _open(config):
    try:
        with open(config, "r") as fh:
            return fh.read()
    except OSError as err:
        print(err)
>>> _open('foo.txt')
'hello'

>>> _open('foo999.txt')
[Errno 2] No such file or directory: 'foo999.txt'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Good class design - with a Snake game as an example bear 1 1,714 Jan-24-2024, 08:36 AM
Last Post: annakenna
  Sound Approach for Running a Shell Command? matt_the_hall 8 3,255 Dec-14-2020, 02:52 PM
Last Post: matt_the_hall
  Whats wrong with the elif? inunanimous93 3 2,409 Nov-30-2020, 03:58 AM
Last Post: deanhystad
  Need feedback on my approach for python dashboard for Asana pashtett 0 1,284 Nov-24-2020, 11:51 AM
Last Post: pashtett
  Whats Wrong!? rjay81 3 2,216 May-13-2020, 08:02 PM
Last Post: rjay81
  Can u see this code and tell whats wrong with it? AhmadKamal 14 5,184 Apr-29-2020, 11:09 AM
Last Post: jefsummers
  Approach to creating Audit table pynewbie 4 3,747 Feb-24-2020, 06:12 PM
Last Post: pynewbie
  list approach due nested order 3Pinter 6 2,739 Oct-07-2019, 01:49 PM
Last Post: 3Pinter
  elevator simulator...whats the wrong at this code? tasos710 5 5,840 Jun-11-2019, 01:38 AM
Last Post: micseydel
  whats the difference between sys.exit() and break? mitmit293 1 4,083 Jan-27-2019, 09:46 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020