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

Conversation

jbwhit
Copy link

@jbwhit jbwhit commented Mar 29, 2014

The with construct is the preferred way of reading and writing
files. Among other things it ensures that the files are closed
automatically. I also wrote it in a python 2/3 agnostic way.

The `with` construct is the preferred way of reading and writing
files. Among other things it ensures that the files are closed
automatically. I also wrote it in a python 2/3 agnostic way.
@lehmannro
Copy link
Contributor

I'm not sure if this is within the scope of this material, let alone this section. Is the goal of this material to make the learner the best, most idiomatic and best practice data programmer or just teach them how they can solve some interesting data puzzles? I'm at least −0 on having this in the core curriculum. What do you think about an appendix chapter?

@@ -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!)

@projectgus
Copy link
Contributor

Hi everyone,

@jbwhit brought this up in yesterday's workshop in Melbourne, and I suggested he put together a PR.

I think the comments raised by @lehmannro are valid too, though. On reflection there is a lot of material and new concepts in this workshop for people who are perhaps only 4 hours into programming (we've found only a small percentage of people make it through the whole of this existing core material in four hours, already.)

I like the suggestion of adding these as an Extras section instead, maybe titled "Advanced File Techniques" or "Additional File Techniques". Under the "Why Do You Use Print..." we can link to that discussion as an optional detour.

What do you think about trying it that way?

@jbwhit
Copy link
Author

jbwhit commented Mar 30, 2014

@lehmannro: I'm not sure if this is within the scope of this material, let alone this section. Is the goal of this material to make the learner the best, most idiomatic and best practice data programmer or just teach them how they can solve some interesting data puzzles? I'm at least −0 on having this in the core curriculum. What do you think about an appendix chapter?

I think think your comments argues for a very different document than what's currently there (which may be your point). If you are arguing that "writing to a file" is outside the scope of what we want to do, then we should simply remove all "getting text to a file" aspects out of the tutorial. But, I wouldn't argue for that, and I suspect you aren't. Let me know if I have this wrong.

So assuming that you want to teach people how to write text to a file: As it stands, the tutorial goes through how to use print to print to stdout. Then it purports to teach how get text that can be readily printed to the screen, into to a file. But here is where it seems to go off the rails, because it says that printing to a file is fundamentally different. You have to write to a file, not print. But not only that, you have to first open the file, write to it, then close it. And you still aren't done because remember how print worked like you expected? Well, write is special, so now you have to append "\n" to each line to get the results that you expect.

Instead, I'm arguing for text that says you can use print("hi") just like before. But instead of the default stdout, you simply have to tell it where to print, so it becomes, print("hi", file=file_handle). I'm happy to modify the PR if you want them to use the syntax of f.open; f.close. The advantage of that is that you don't have to use the with statement construct.

P.S. I don't know what -0 means, but I can guess that it's not supportive.

@jbwhit
Copy link
Author

jbwhit commented Mar 30, 2014

One final simple option is:

  • use the print() as before (ignore 2.7 incompatibility without the extra import)
  • remove the current "write" section entirely
  • use print("hi", file=filehandle) with filehandle.open/close

Disadvantages:

  • no section talking about line terminals like "\n"
  • The current suggestion to use anaconda to download python environment currently clashes with 3.x syntax used everywhere because it (currently) only ships with 2.7

@jbwhit
Copy link
Author

jbwhit commented Apr 2, 2014

@lehmannro do you want me to revise the "writing" to a file section of the webpage to only mention "print"ing to a file and resubmit the pull request?

@Ivoz
Copy link
Member

Ivoz commented Apr 17, 2014

I feel mentioning write as a core method of putting data into a file (and differentiating it from print) is warranted. This comes from the common use cases being different - in simple console output, 95% of the time you wish to print a line. For files, you may want to write arbitrary data, and sometimes not want an implicit newline, whether you're writing text or binary data.

I'd also prefer including the with construct as the default method of accessing / open files, since its recommended for both 3.x and 2.7 which is what anyone new to python should be using. Since people are learning new functionality here anyway, I don't think the introduction of the construct is too much of a strain on people as opposed to purely the introduction of the open function. It's also teaching people the right way, first.

What doing the above, could additionally do is make usage of the print(x, file=handle) construct a much more natural addendum (e.g "footnote: use print... with this extra keyword to get automatic newlines!).

@benoitbleuze
Copy link
Member

I support the use of write as the concept in opposition to using print. The explanation from Matt on that point doesn't need to furthered in any way.

As for the with, at first I believed it boiled down to what we want to teach: pure modern Python data IO programming, or data IO programming concepts using python.
The open()/close() construct has been a constant over most programming languages for decades. Learners will get to know about file IO close to system calls that will help them in whatever language the want to use in the future.

But since then I revised my judgment: we need the learners to understand what they are doing, not just be robots that know a few functions and can't see what is happening behind the with.
Mentioning both constructs: using with, and along with it open() seems natural as you explain what with does.

I would first introduce the with, and then detail what happens, including the exception and the close call. This way they will learn what is happening and how to open and close files manually if they need to, regardless of whether it's python or not.

I had the case of one of my developers last week who had a crash due to a related error. He did open files in a loop using with, and put the FILE objects in a list, then returning that list and building a multipart data form with all the FILE objects. Of course his file objects were pointing to closed files outside of his loop, and he couldn't guess why. This anecdote show the relevance of knowing the concepts of opening and closing, that are required even if you use the new with construct.

Happy easter holiday to those preparing to a long week end

Ben.

@ellenkoenig
Copy link
Contributor

Has been fixed recently.

@jbwhit
Copy link
Author

jbwhit commented May 17, 2018

I'm glad to hear that the with construct became the way that we recommended in the end! I see that it's used well here: http://opentechschool.github.io/python-data-intro/core/survey.html although, perhaps confusingly, it doesn't appear to be how we recommend things here: http://opentechschool.github.io/python-data-intro/core/csv.html

Best of luck!

@ellenkoenig
Copy link
Contributor

ellenkoenig commented May 17, 2018

Don’t worry, we’ll get to the CSV part later in our efforts to update the curriculum 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants