Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17781 > unrolled thread

Re: Idiom for shelling out to $EDITOR/$PAGER?

Started byPeter Otten <__peter__@web.de>
First post2011-12-23 09:28 +0100
Last post2011-12-23 09:47 +0000
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Idiom for shelling out to $EDITOR/$PAGER? Peter Otten <__peter__@web.de> - 2011-12-23 09:28 +0100
    Re: Idiom for shelling out to $EDITOR/$PAGER? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-23 09:44 +0000
      Re: Idiom for shelling out to $EDITOR/$PAGER? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-23 09:47 +0000

#17781 — Re: Idiom for shelling out to $EDITOR/$PAGER?

FromPeter Otten <__peter__@web.de>
Date2011-12-23 09:28 +0100
SubjectRe: Idiom for shelling out to $EDITOR/$PAGER?
Message-ID<mailman.4021.1324628949.27778.python-list@python.org>
Tim Chase wrote:

> After a little searching, I've not been able to come up with what
> I'd consider canonical examples of consider calling an external
> editor/pager on a file and reading the results back in.  (most of
> my results are swamped by people asking about editors written in
> Python, or what the best editors for Python code are)
> 
> The pseudocode would be something like
> 
>    def edit_text(data):
>      temp_fname = generate_temp_name()
>      try:
>        f = file(temp_fname, 'w')
>        f.write(data)
>        f.close()
>        before = info(temp_fname) # maybe stat+checksum?
>        editor = find_sensible_editor()
>        subprocess.call([editor, temp_fname])
>        if before == info(temp_fname):
>          return None
>        else:
>          return file(temp_fname).read()
>      finally:
>        delete_if_exists(temp_fname)
> 
> However there are things to watch out for in this lousy code:
> 
> -race conditions, unique naming, and permissions on the temp file
> 
> -proper & efficient detection of file-change, to know whether the
> user actually did anything

Just read the whole thing back into memory and compare the string to the 
original data. The file has to be quite long for the checksum calculation to 
be worthwhile.

> -cross-platform determination of a sensible editor (that blocks
> rather than spawns), using platform conventions like
> os.environ['EDITOR']
> 
> -cleanup deletion of the temp-file
> 
> I presume the code for spawning $PAGER on some content would look
> pretty similar.
> 
> Any good example code (or blog posts, or other links) that has
> been battle-tested?

You could look into mercurial to see what it does to let you edit its commit 
messages. A quick look into mercurial/ui.py 
(http://selenic.com/hg/file/9cf1620e1e75/mercurial/ui.py, start with the 
ui.edit() method) suggests that it uses the same approach as your 
pseudocode.

[toc] | [next] | [standalone]


#17786

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-12-23 09:44 +0000
Message-ID<4ef44d7f$0$29973$c3e8da3$5496439d@news.astraweb.com>
In reply to#17781
On Fri, 23 Dec 2011 09:28:52 +0100, Peter Otten wrote:

>> -proper & efficient detection of file-change, to know whether the user
>> actually did anything
> 
> Just read the whole thing back into memory and compare the string to the
> original data. The file has to be quite long for the checksum
> calculation to be worthwhile.

I don't think so. No matter how long the file is, the checksum is always 
going to do far more work than a simple string comparison. A string 
comparison can short-circuit on the first difference; the checksum must 
always process both files in their entirety.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#17787

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-12-23 09:47 +0000
Message-ID<4ef44e1a$0$29973$c3e8da3$5496439d@news.astraweb.com>
In reply to#17786
On Fri, 23 Dec 2011 09:44:31 +0000, Steven D'Aprano wrote:

> On Fri, 23 Dec 2011 09:28:52 +0100, Peter Otten wrote:
> 
>>> -proper & efficient detection of file-change, to know whether the user
>>> actually did anything
>> 
>> Just read the whole thing back into memory and compare the string to
>> the original data. The file has to be quite long for the checksum
>> calculation to be worthwhile.
> 
> I don't think so. No matter how long the file is, the checksum is always
> going to do far more work than a simple string comparison. A string
> comparison can short-circuit on the first difference; the checksum must
> always process both files in their entirety.

Wait... the above only holds assuming you keep both the before-editing 
and after-editing versions. If you don't, then Peter is right, there 
comes a point where keeping a checksum saves enough memory for the 
additional effort to be worth while.

Sorry for the noise.


-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web