New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
codecs.StreamWriter.writelines problem when passed generator #49695
Comments
This is the implementation of codecs.Streamwriter.writelines for all def writelines(self, list):
This may be a problem if the 'list' parameter is a generator. The def writelines(self, list):
If a caller knows that it's input list would use a reasonable amount |
When fixing this, note that the builtin name "list" should not be |
For the common case where list is in fact a sequence of strings, the Note that the method doesn't pretend to support generators for the list Furthermore, the StreamWriter method can easily be overridden by the IMHO, it's better to use the .write() method in a for-loop of your |
In Python's file protocol, readlines and writelines is a protocol for If writelines is using join, then if one passes an iterator as the I think following the protocol is more important than performance. If However, here is an alternative implementation that is slightly more
def writelines(self, sequence):
I'm not sure which is better. But one last point is that Python is |
Let me give an example of why it's important that writelines rows = (line[:-1].split('\t') for line in in_file)
projected = (keep_fields(row, 0, 3, 7) for row in rows)
filtered = (row for row in projected if row[2]=='1')
out_file.writelines('\t'.join(row)+'\n' for row in filtered) For a large input file, for a regular out_file object, this will work. |
On 2009-03-10 16:36, Daniel Lescohier wrote:
Of course, it's possible to have a generator producing lots of data, If you'd like to see such support in .writelines(), please provide The standard case of passing a list of strings to that method BTW: I am not aware of any .writelines() file protocol. If there Thanks,Marc-Andre Lemburg ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 |
OK, I think I see where I went wrong in my perceptions of the file So, the StreamWriter interface matches the file protocol; readlines() Then, the example I wrote would be instead: rows = (line[:-1].split('\t') for line in in_file)
projected = (keep_fields(row, 0, 3, 7) for row in rows)
filtered = (row for row in projected if row[2]=='1')
formatted = (u'\t'.join(row)+'\n' for row in filtered)
write = out_file.write
for line in formatted:
write(line) I think it's correct that the file object write C code only does |
I find this to be a confusing issue.
If io.writelines does the same (concatenate all together), it should say so, but one comment suggests it does not
|
On 25.03.2013 23:11, Terry J. Reedy wrote:
The doc string of the base class merely describes what the In one of the previous messages, I've already outlined a workable Since no patch has since been submitted, I'd suggest to close the |
Agreed. Modern consumers can use the io module. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: