Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60811 > unrolled thread
| Started by | Eamonn Rea <eamonnrea@gmail.com> |
|---|---|
| First post | 2013-11-30 14:45 -0800 |
| Last post | 2013-12-01 00:51 +0000 |
| Articles | 10 — 5 participants |
Back to article view | Back to comp.lang.python
Change a file type in Python? Eamonn Rea <eamonnrea@gmail.com> - 2013-11-30 14:45 -0800
Re: Change a file type in Python? Chris Angelico <rosuav@gmail.com> - 2013-12-01 10:52 +1100
Re: Change a file type in Python? Eamonn Rea <eamonnrea@gmail.com> - 2013-11-30 16:04 -0800
Re: Change a file type in Python? rusi <rustompmody@gmail.com> - 2013-11-30 19:02 -0800
Re: Change a file type in Python? Chris Angelico <rosuav@gmail.com> - 2013-12-01 14:22 +1100
Re: Change a file type in Python? rusi <rustompmody@gmail.com> - 2013-11-30 20:58 -0800
Re: Change a file type in Python? Chris Angelico <rosuav@gmail.com> - 2013-12-01 16:03 +1100
Re: Change a file type in Python? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-01 11:20 +0000
Re: Change a file type in Python? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-01 11:20 +0000
Re: Change a file type in Python? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-01 00:51 +0000
| From | Eamonn Rea <eamonnrea@gmail.com> |
|---|---|
| Date | 2013-11-30 14:45 -0800 |
| Subject | Change a file type in Python? |
| Message-ID | <d3c7a900-b44a-45da-bab2-9625c01471a7@googlegroups.com> |
When opening a file, you'd say whether you want to read or write to a file. This is fine, but say for example later on in the program I change my mind and I want to write to a file instead of reading it. Yes, I could just say 'r+w' when opening the file, but what if I don't know if I'm going to do both? What if I have the user decide, and then later on I let the user change this. Is it possible to do so without opening the file again and using the same file object? Thanks!
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-12-01 10:52 +1100 |
| Message-ID | <mailman.3428.1385855538.18130.python-list@python.org> |
| In reply to | #60811 |
On Sun, Dec 1, 2013 at 9:45 AM, Eamonn Rea <eamonnrea@gmail.com> wrote: > Is it possible to do so without opening the file again and using the same file object? In the general sense, no, but you may be able to abuse things terribly by calling __init__ on an existing object. The only advantage of that would be if you have multiple references to the file object, so normally don't - just close it and reopen. You can't change access/share mode on the file system without closing and reopening, so ultimately that's going to have to happen. As a separate point, can you please use a better client than Google Groups? It's buggy and your posts come out looking ugly. There are other news clients, or you can sign up for the email list here: https://mail.python.org/mailman/listinfo/python-list ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Eamonn Rea <eamonnrea@gmail.com> |
|---|---|
| Date | 2013-11-30 16:04 -0800 |
| Message-ID | <db803ec5-9d07-441c-9fa4-ea9ce7292d14@googlegroups.com> |
| In reply to | #60813 |
Thanks for the help! Ok, I'll look into the mailing list.
[toc] | [prev] | [next] | [standalone]
| From | rusi <rustompmody@gmail.com> |
|---|---|
| Date | 2013-11-30 19:02 -0800 |
| Message-ID | <37c37e04-87f1-42d3-ae24-0c159281aa46@googlegroups.com> |
| In reply to | #60814 |
On Sunday, December 1, 2013 5:34:11 AM UTC+5:30, Eamonn Rea wrote:
> Thanks for the help!
>
> Ok, I'll look into the mailing list.
[Assuming you are using GG with firefox on linux]
All you need to do is
1. Install 'Its all text' FF addon
2. Point the 'editor' of 'Its all text' to the below python script
After that, a small new edit button will appear outside the text-box and its one-click solution
-------------Python Script--------------
#!/usr/bin/env python3
# As far as I know both python2 and 3 work
# Windows/Mac no idea :-)
# A script to drop-in as an editor for firefox addon "Its all text"
# It cleans up two google-group nuisances:
# 1. Useless blank lines
# 2. Excessively long lines
# No efforts at error reporting as stderr is not available in any
# easy way (I know) to firefox (other browsers?)
# To test separately:
# Compose a mail (preferably reply) in GG
# Copy-paste the stuff (maybe with some long lines added without the >)
# Run this script with that filename as argv[1]
from sys import argv
from re import sub
import re
# Clean double spacing
def cleands(s):
# Assumption: ASCII 025 (NAK) never occurs in input
s1 = sub("^> *\n> *$", "\025" , s , flags=re.M)
s2 = sub("^> *\n" , "" , s1, flags=re.M)
s3 = sub("\025\n" , ">\n" , s2, flags=re.M)
return s3
# Maximum length that (new) lines should attain
Maxlen = 75
# clean all long lines, s is the whole file/text
def cleanall_ll(s):
lines = (cleanll(l) for l in s.split("\n"))
return "\n".join(lines)
# clean one long line
def cleanll(line):
return ( line if line.startswith(">") else cleanll_rec(line) )
def cleanll_rec(line):
if len(line) <= Maxlen : return line
pos = line.rfind(" ", 0, Maxlen)
if pos == -1 : #Failed due to no spaces
return line
return line[0:pos] + "\n" + cleanll_rec(line[pos+1: ])
def clean(s):
return cleanall_ll(cleands(s))
def main():
with open(argv[1]) as f: s = f.read()
with open(argv[1], "w") as f: f.write(clean(s))
if __name__ == '__main__' :
main()
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-12-01 14:22 +1100 |
| Message-ID | <mailman.3434.1385868471.18130.python-list@python.org> |
| In reply to | #60823 |
On Sun, Dec 1, 2013 at 2:02 PM, rusi <rustompmody@gmail.com> wrote: > On Sunday, December 1, 2013 5:34:11 AM UTC+5:30, Eamonn Rea wrote: >> Thanks for the help! >> >> Ok, I'll look into the mailing list. > > [Assuming you are using GG with firefox on linux] > > All you need to do is > 1. Install 'Its all text' FF addon > 2. Point the 'editor' of 'Its all text' to the below python script > > After that, a small new edit button will appear outside the text-box and its one-click solution Yeah I still think it's a lot easier to switch to a properly-working system. What you're suggesting still doesn't wrap text properly, as evidenced by your above over-long line. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | rusi <rustompmody@gmail.com> |
|---|---|
| Date | 2013-11-30 20:58 -0800 |
| Message-ID | <b3249ec1-1ae7-4f15-b127-14cc3bdd1608@googlegroups.com> |
| In reply to | #60825 |
On Sunday, December 1, 2013 8:52:03 AM UTC+5:30, Chris Angelico wrote: > On Sun, Dec 1, 2013 at 2:02 PM, rusi wrote: > > On Sunday, December 1, 2013 5:34:11 AM UTC+5:30, Eamonn Rea wrote: > >> Thanks for the help! > >> > >> Ok, I'll look into the mailing list. > > > > [Assuming you are using GG with firefox on linux] > > > > All you need to do is > > 1. Install 'Its all text' FF addon > > 2. Point the 'editor' of 'Its all text' to the below python script > > > > After that, a small new edit button will appear outside the text-box and its one-click solution > > What you're suggesting still doesn't wrap text properly, as > evidenced by your above over-long line. > > ChrisA Ok my bad. I offered a 1-click solution, but did 0 clicks :-) Strictly speaking one needs anywhere between one and two clicks for this to work properly. My profuse apologies for the improper and illegitimate round-down <wink> > Yeah I still think it's a lot easier to switch to a properly-working > system. Of course -- if 1.something clicks are too onerous, you are free not to use it :-) More seriously this discussion completely misses the point. I think we are dealing with 3 completely separable problems: [Slightly changing what I earlier wrote…] 1. Undesirable elements -- spam, troll and more exotic 2. Immature noobs -- literally or almost literally kids 3. Stupid technology -- in this case, GG The anti-GG crusade is getting pissed-off with 1 and/or 2 and then attacking 3.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-12-01 16:03 +1100 |
| Message-ID | <mailman.3435.1385874207.18130.python-list@python.org> |
| In reply to | #60826 |
On Sun, Dec 1, 2013 at 3:58 PM, rusi <rustompmody@gmail.com> wrote: > I think we are dealing with 3 completely separable problems: > [Slightly changing what I earlier wrote…] > > 1. Undesirable elements -- spam, troll and more exotic > 2. Immature noobs -- literally or almost literally kids > 3. Stupid technology -- in this case, GG > > The anti-GG crusade is getting pissed-off with 1 and/or 2 and then > attacking 3. Most of it is getting annoyed at the results of 3, and then attacking 3. That's what I do, at least. There have been several people who've switched to email as a result of being told that their posts are coming out looking ugly; their posts subsequently are NOT ugly, and they always had useful content in them, so they become productive and highly welcome members of the community without being masked behind buggy technology. Nothing to do with immaturity or spam. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-12-01 11:20 +0000 |
| Message-ID | <529b1b72$0$29993$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #60827 |
On Sun, 01 Dec 2013 16:03:17 +1100, Chris Angelico wrote: > Most of it is getting annoyed at the results of 3, and then attacking 3. I know the feeling. I've never trusted 3, I've always felt that it's plotting something. And it looks like half an 8, but it's not. What's with that? -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-12-01 11:20 +0000 |
| Message-ID | <mailman.3436.1385896835.18130.python-list@python.org> |
| In reply to | #60814 |
On 01/12/2013 00:04, Eamonn Rea wrote: > Thanks for the help! > > Ok, I'll look into the mailing list. > It's very useful, you can even see things in context, which is conspicious by its absence above :) -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-12-01 00:51 +0000 |
| Message-ID | <529a880f$0$29993$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #60811 |
On Sat, 30 Nov 2013 14:45:18 -0800, Eamonn Rea wrote: > When opening a file, you'd say whether you want to read or write to a > file. This is fine, but say for example later on in the program I change > my mind and I want to write to a file instead of reading it. Yes, I > could just say 'r+w' when opening the file, but what if I don't know if > I'm going to do both? What if I have the user decide, and then later on > I let the user change this. I'd say if you're worried about this, your program needs to be redesigned. In general, with the exception of special purpose files like databases (and you're not inventing your own database, I hope) it is good clean programming practice to keep files open only so long as you really need them to be open. - It's only possible to have open some number of files open at a time. That number is typically quite large, but not *that* large. Every time you open a file and keep it open, it impacts other programs and the operating system by a small amount. - Particularly on Windows, once you open a file, nothing else can open it. That means it can't be backed up, scanned for viruses, opened by other programs, deleted or renamed. - When you open a file for writing, and keep it open, the changes you make aren't guaranteed to be written to disk until you actually close the file. You can call the sync method, the longer you keep it open the more likely the risk of data-loss in the case of sudden crash or power interruption. - To avoid data loss, you should try to avoid updating files in place. If the program crashes, you could leave the file in a messed up state. With very few exceptions, I recommend that you avoid this approach: # Don't do this. open file read data process in memory write changes to file more processing write changes to file more processing write changes to file close file exit in favour of this approach: open file; read data; close file process in memory open file; write to file; close file more processing open file; write to file; close file more processing open file; write to file; close file exit > Is it possible to do so without opening the file again and using the > same file object? No. You cannot even re-open a file object using the same file mode. Why do you care? It isn't difficult to throw away the file object and create a new one. That part is cheap. -- Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web