Python Forum
returning a different class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
returning a different class
#1
i am rewriting my zopen class. this time i want to catch the case of a file which needs no special handling an just return the file object my __init__() gets, instead of itself. my thought is to implement the class as another name and a new function as the original name and have the function determine which can/should be returned and call the new name for the class or just open the file as appropriate and return whichever open file object it got. does this seem like to way to go? i am asking because i am unsure and this would involve a lot of rewriting.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
It does look like the way to go. We already discussed this and it seems to be just a case of the Factory Pattern.
Reply
#3
what i ended up doing was adding another method that tests for a single file and not doing a tempfile (which needs to be renamed at close() time). if so, it returns the referenced file, else it returns self. the function zopen() effectively does return _zopen(*args,**kwargs).just_one_file() (coded with explicit args). so the wrapper function was easy to do. the code was simpler this way. and it works.
#-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------
# return just one file to be used
#-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------
# if _zopen() only needs to open and use a single file then the caller
# can just use that one file directly.  method just_one_file() returns
# that one open file.  function zopen calls just_one_file() to replace
# the _zopen() object.  method just_one_file() will return the same
# _zopen() object if it is to be used.  even though zopen() is a
# function, it returns the appropriate class instance to use.
#-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------
    def just_one_file(self):
        """Return just the one file to be used."""
        if len(self.filestack) == 1 and not self.tempname:
            file = self.filestack[0]
            self.filestack[0] = None
            self.filestack = None
            self.ready = False
            return file
        return self
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
(Oct-11-2021, 08:32 PM)Skaperen Wrote: this would involve a lot of rewriting.

You do have tests around your code, right? If not, you really should as tests allow you to make changes with confidence - you'll know quickly if you've broken something.
Reply
#5
tests are being developed in parallel. it's not a done project until all features and aspects have a working test.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

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