Jun-30-2020, 02:17 AM
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:
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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.