Python Forum
how to get around recursive method call
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to get around recursive method call
#3
i tried to juggle things around with a __getattr__() method and finally eliminated the recursion. but it still ran a finite 2 times. no idea why that happens. but i will try __getattribute__() and see if i can make that work.

the class is emulating open() and is intercepting close(). it needs to pass everything else straight through. it's a no-compress version of the class described here.

i know you want to see my code. please focus on this issue in this thread.

this it class topen in file "topen.py":
import io,os,sys,time

_separator = '_'

class topen(io.IOBase):
    """Class for opening a file with temporary output name and automatic rename on close if write."""
    # this class also adds the name= keyword argument (no default)
    # name is require by positional args or by keyword args
    # this class is like ztopen but without compression

    def __init__(self,*args,**kwargs):

        if args:
            args = list(args)
            if 'name' in kwargs:
                raise TypeError('name given twice')
            fname = args.pop(0)
            if args:
                if 'mode' in kwargs:
                    raise TypeError('mode given twice')
                modes = args.pop(0)
            elif 'mode' in kwargs:
                modes = kwargs.pop('mode')
            else:
                raise TypeError('mode not given')
        elif 'name' in kwargs:
            fname = kwargs.pop('name')
            modes = kwargs.pop('mode','r')
        else:
            raise TypeError('name not given')

        if 'x' in modes and isinstance(fname,(str,bytes)) and os.path.exists(fname):
            raise TypeError(f'{fname!r} refers to a name that already exists')

        fname = os.fspath(fname)
        if isinstance(fname,bytes):
            fname = ''.join(chr(x)for x in fname)
        if isinstance(modes,bytes):
            modes = ''.join(chr(x)for x in modes)

        if isinstance(fname,str) and '+' not in modes:
            rname = 'x' in modes or 'w' in modes
            oname = fname+_separator+str(int(time.time()*3906250)) if rname else fname
        elif isinstance(fname,int):
            rname = False
            oname = fname
        else:
            raise TypeError()

        print(f'opening file {oname!r} with mode {modes!r}',file=sys.stderr,flush=1)
        ofile = open(oname,modes,*args,**kwargs)
        if ofile is None:
            raise(f'failed to open file {oname!r}')

        self.rname = rname
        self.ofile = ofile
        self.oname = oname
        self.fname = fname

    def close(self):
        if self.rname:
            print(f'close {self.oname!r} and rename it to {self.fname!r}',file=sys.stderr,flush=1)
            self.ofile.close()
            return os.rename(self.oname,self.fname)
        else:
            print(f'close {self.oname!r}',file=sys.stderr,flush=1)
            return self.ofile.close()

    def __getattr__(self,*args):
        print(f'__getattr__ for {args!r}',file=sys.stderr,flush=1)
        return getattr(self.ofile,*args)
here is the test script in file "trytopen.py":
from topen import topen
f=topen('foo','w')
print('bar',file=f)
f.close()
i plan to try with with in test 2 or 3.

here is the output i get:
Output:
lt2a/phil /home/phil 188> py trytopen.py opening file 'foo_6224487216638762' with mode 'w' __getattr__ for ('write',) __getattr__ for ('write',) close 'foo_6224487216638762' and rename it to 'foo' __getattr__ for ('__IOBase_closed',) close 'foo_6224487216638762' and rename it to 'foo' lt2a/phil /home/phil 189>
Tradition is peer pressure from dead people

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


Messages In This Thread
RE: how to get around recursive method call - by Skaperen - Jun-29-2020, 09:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  method call help sollarriiii 6 1,324 Feb-21-2023, 03:19 AM
Last Post: noisefloor
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 7,728 Jan-17-2021, 03:02 AM
Last Post: Jeremy7
  list call problem in generator function using iteration and recursive calls postta 1 2,030 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  Return boolean from recursive class method medatib531 6 3,690 Jul-13-2020, 04:27 AM
Last Post: medatib531
  How to call COM-method using comtypes jespersahner 0 2,517 Nov-15-2019, 12:54 PM
Last Post: jespersahner
  Polymorphism not working with a call to a abstract method colt 3 2,426 Nov-04-2019, 11:04 PM
Last Post: colt
  How to Call a method of class having no argument dataplumber 7 6,749 Oct-31-2019, 01:52 PM
Last Post: dataplumber
  Call method from another method within a class anteboy65 3 7,679 Sep-11-2019, 08:40 PM
Last Post: Larz60+
  What is the use of call method and when to use it? everyday1 1 3,389 Jul-14-2019, 01:02 PM
Last Post: ichabod801
  I'm trying to figure out whether this is a method or function call 357mag 2 2,532 Jul-04-2019, 01:43 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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