Re: [Python-ideas] Rewriting file - pythonic way

2018-04-16 Thread Alexey Shrub

https://pypi.python.org/pypi/in-place
> * Instead of hijacking sys.stdout, a new filehandle is returned for 
writing.
> * The filehandle supports all of the standard I/O methods, not just 
readline().


why fileinput did not support this things?

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Rewriting file - pythonic way

2018-04-16 Thread Alexey Shrub
В Понедельник, 16 апр. 2018 в 2:48 , Alexey Shrub 
<ash...@yandex.ru> написал:

https://pypi.python.org/pypi/in-place


I like in_place module
https://github.com/worldmind/scripts/blob/master/filerewrite/inplacetest.py
it fix some strange features of fileinput module.
Maybe in_place must be in standard library instead fileinput?

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Rewriting file - pythonic way

2018-04-16 Thread Alexey Shrub
В Воскресенье, 15 апр. 2018 в 10:47 , George Fischhof 
 написал:

https://docs.python.org/3/library/fileinput.html


https://pypi.python.org/pypi/in-place
looks not bad too
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Rewriting file - pythonic way

2018-04-16 Thread Alexey Shrub
В Воскресенье, 15 апр. 2018 в 1:12 , Serhiy Storchaka 
 написал:
Actually the reliable code should write into a separate file and 
replace

the original file by the new file only if writing is successful. Or
backup the old file and restore it if writing is failed. Or do both. 
And
handle hard and soft links if necessary. And use file locks if needed 
to
prevent race condition when read/write by different processes. 
Depending

on the specific of the application you may need different code. Your
three lines are enough for a one-time script if the risk of a powerful
blackout or disk space exhaustion is insignificant or if the data is 
not

critical.


I not sure that solving described problems is a task of this level, 
maybe it problem for higher level


___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Rewriting file - pythonic way

2018-04-16 Thread Alexey Shrub
В Воскресенье, 15 апр. 2018 в 10:47 , George Fischhof 
 написал:

https://docs.python.org/3/library/fileinput.html


Thanks, it works
https://github.com/worldmind/scripts/blob/master/filerewrite/fileinputtest.py
but looks like that way only for line by line processing

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Rewriting file - pythonic way

2018-04-16 Thread Alexey Shrub
В Воскресенье, 15 апр. 2018 в 6:19 , Oleg Broytman 
 написал:

Can I recommend to catch exceptions in `backuper.backup()`,
cleanup backuper and unlock locker?


Yes, thanks, I move .backup() to try, about other exception I think 
that it must be catched outside, because this module don't know that to 
do with such problems


___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Rewriting file - pythonic way

2018-04-15 Thread Alexey Shrub
В Воскресенье, 15 апр. 2018 в 2:40 , Nick Coghlan 
 написал:

https://bugs.python.org/issue8604#msg174104 is the relevant tracker
discussion


Thanks all, I agree that universal and absolutly safe solution is very 
difficult, but for experiment I made some draft

https://github.com/worldmind/scripts/tree/master/filerewrite
main code here
https://github.com/worldmind/scripts/blob/master/filerewrite/filerewrite.py#L46

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Rewriting file - pythonic way

2018-04-15 Thread Alexey Shrub
В Воскресенье, 15 апр. 2018 в 12:40 , Serhiy Storchaka 
 написал:

If the problem is that you want to use a single line instead of three
line, you can add a function


Yes, I think that single line with word 'rewrite' is much more readable 
than those three lines.
And yes, I can make my own function, but it is typical task - maybe it 
must be in standard library?


___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Rewriting file - pythonic way

2018-04-15 Thread Alexey Shrub

Hi all,

I am new in python (i am moving from Perl world), but I always love 
Python for hight level, beatuful and clean syntax.

Now I have question/idea about working with files.
On mine opinion it very popular use case:
1. Open file (for read and write)
2. Read data from file
3. Modify data.
4. Rewrite file by modified data.

But now it is looks not so pythonic:

with open(filename, 'r+') as file:
   data = file.read()
   data = data.replace('old', 'new')
   file.seek(0)
   file.write(data)
   file.truncate()

or something like this

with open(filename) as file:
   data = file.read()
data = data.replace('old', 'new')
with open(filename) as file:
   file.write(data)

I think best way is something like this

with open(filename, 'r+') as file:
   data = file.read()
   data = data.replace('old', 'new')
   file.rewrite(data)

but for this io.BufferedIOBase must contain rewrite method

what you think about this?



___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/