OpenTechSchool / python-data-intro

Workshop material for "Introduction to Data Processing with Python"
http://opentechschool.github.io/python-data-intro/
Other
40 stars 18 forks source link

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

Closed jbwhit closed 6 years ago

jbwhit commented 10 years ago

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 commented 10 years ago

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?

projectgus commented 10 years ago

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 commented 10 years ago

@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 commented 10 years ago

One final simple option is:

Disadvantages:

jbwhit commented 10 years ago

@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 commented 10 years ago

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 commented 10 years ago

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 commented 6 years ago

Has been fixed recently.

jbwhit commented 6 years ago

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 commented 6 years ago

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