Skip to content

Added a section of text about using the with construct. #6

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion core/text-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,25 @@ If you want to try all this out, here's a quick exercise to make sure you've got

When writing to the files, remember that `print()` adds a newline but with `write()` you have to add the newline yourself.

Hopefully pretty simple, but that should make sure you have all the above ideas down-pat.
Hopefully pretty simple, but you should make sure you have all the above ideas down-pat.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is not from your PR, but while we're at it we could maybe remove the passive-aggressive tone in that? It implies you're somehow not fit as a learner to continue if you did not understand all of this by your heart.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. I just jumped in and did this one as 4dc8aa9 (will probably cause a merge conflict, sorry!)


### Python 2.x and 3.x

It's useful to have seen everything above, but there is a preferred way to handle these intricacies. It uses a python reserved word: `with`. Unfortunately, python is in a transition period between 2.7 and 3.x, so there is an added complication. This tutorial is based on 2.7, while the future will be 3.x. The good news is, you can properly write code that will work in both versions by adding the following line to 2.7 code once (near the top of your script with other imports):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, I know same-source multi-version compatibility is a hard topic but this discussion seems like it's boring and out of scope for the "data processing" discussion. Is there a way to make this easier, maybe by just proposing to add it by default (enable it, so to say) and then adding a hint/disclaimer for 3.x that you can leave it out? This seems pretty lengthy and boring right now.


from __future__ import absolute_import, division, print_function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why exactly do we need division and print_function here as well?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need absolute_import and division (you do need the print_function). The reason for including both came the answers I got from asking the question: "how do I make it so that python code that I write in 2.7 will transfer to 3.x".

There are two ways to go about this:

  1. do the minimum, i.e., include only print_function -- and make a caveat that this will only force conversion for "print" statements in python and if write other functions that are not 3.x compliant, you have to add extra imports (and give the extra imports in a note or tell them to google it themselves). If you don't make this caveat, the impression will be that this will "3-proof" their code, when it won't.

or

  1. Include the most general form of what to include that catches almost all things that would get caught in a transfer from 2.7 to 3.x; and tell them to simply include this general one line to the top of your python scripts. This approach seems much simpler and clearer, and the reason I went with it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're going to do one, there's almost no reason not to do the others (at least, if you're writing novel code). That way you're more systematically trying to be python 3 compatible, rather than selectively.


If you are using version 3.0 (or a higher number), feel free to leave it out (though it doesn't hurt to leave it in, either). But, assuming you are using 2.7, see how to print an example file below:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if "print an example file" is language non-programmers would use/understand. Maybe "write" is a better fit?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except, in contrast to the section above, where you are actually using write to write to the file, while here we are using print to ____ to the file. I chose the word "print" to fill in the blank; do you think it is less confusing to use "write" in that context?


from __future__ import absolute_import, division, print_function
# ... any other code you want here ...
with open("extrafile.txt", "w") as file_handle:
print("First line", file=file_handle)
print("Second line", file=file_handle)
print("Third line", file=file_handle)

Notice a couple of things with this construct. First, the `with` opens a new code block. The nice thing about the way this is handled is that it will automatically close the file (even if an exception is raised). Second, we can use `print` to print (without a "\n" character specfied), but we have to tell `print` where the text is going (in this case the `file_handle`).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The causal link between "opens a new code block" and "automatically close the file" is not clear to me.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it more clear if reword it this way:

First, the with opens a new code block. The nice thing about using a with statement in this way is that it will automatically close the file (even if an exception is raised).



## Next Chapter

Expand Down