paul j3 added the comment:

An alternative way to delay the file opening, is to return an instance that has 
a `filename` attribute, and has an `open` method.  This can be compactly added 
to the `FileContext` that I defined in the previous patch.  The `FileContext` 
provides the `_ostest` function (testing using os.access), and wrapper for 
stdin/out.


    class FileOpener(argparse.FileContext):
        # delayed FileType; alt to use of partial()
        # sample use:
        # with args.input.open() as f: f.read()
        def __call__(self, string):
            string = self._ostest(string)
            self.filename = string
            return self
        def open(self):
            return self.__delay_call__(self.filename)()
        file =  property(open, None, None, 'open file property')

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue13824>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to