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
#4
i added some more points of output and made repeated calls in a loop. now it shows things that are more strange. the extra 2nd call of close() happens in the next loop cycle or for the last one, after the script reached its end.

the code has been updated.

topen.py:
# -*- coding: utf-8 -*-
import io,os,sys,time

def _sep():
    return '_'+str(int(time.time()*3906250))

class topen(io.IOBase):
    """Class for opening a file with temporary output name and automatic rename on close if write."""
    # 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)
            fname = args.pop(0)
            if args:
                modes = args.pop(0)
            else:
                modes = kwargs.pop('mode',None) 
        else:
            fname = kwargs.pop('file',None)
            modes = kwargs.pop('mode',None)

        if fname is None:
            raise('file is missing')
        if modes is None:
            raise('mode is missing')

        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 ('x' in modes or 'w' in modes) and isinstance(fname,str):
            rname = 1
            oname = fname+_sep()
        else:
            rname = 0
            oname = fname

        args = [oname,modes]+list(args)

        print(f'CALLING open(*{args!r},**{kwargs!r}',file=sys.stderr,flush=1)
        ofile = open(*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)
trytopen.py:
# -*- coding: utf-8 -*-
from sys import stderr

from topen import topen
for n in range(4):
    print(f'--------------------------------------------------- start{n}',file=stderr,flush=1)
    f=topen(f'foo{n}','w')
    print(f'bar{n}',file=f)
    f.close()
    print(f'---------------------------------------------------- done{n}',file=stderr,flush=1)

print('ALL DONE NOW ENDED',file=stderr,flush=1)
topen.out:
Output:
--------------------------------------------------- start0 CALLING open(*['foo0_6224542024360958', 'w'],**{} close 'foo0_6224542024360958' and rename it to 'foo0' ---------------------------------------------------- done0 --------------------------------------------------- start1 CALLING open(*['foo1_6224542024362419', 'w'],**{} close 'foo0_6224542024360958' and rename it to 'foo0' close 'foo1_6224542024362419' and rename it to 'foo1' ---------------------------------------------------- done1 --------------------------------------------------- start2 CALLING open(*['foo2_6224542024363304', 'w'],**{} close 'foo1_6224542024362419' and rename it to 'foo1' close 'foo2_6224542024363304' and rename it to 'foo2' ---------------------------------------------------- done2 --------------------------------------------------- start3 CALLING open(*['foo3_6224542024364380', 'w'],**{} close 'foo2_6224542024363304' and rename it to 'foo2' close 'foo3_6224542024364380' and rename it to 'foo3' ---------------------------------------------------- done3 ALL DONE NOW ENDED close 'foo3_6224542024364380' and rename it to 'foo3'
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-30-2020, 02:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  method call help sollarriiii 6 1,325 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,519 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,754 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