Conversation
mmcky
left a comment
There was a problem hiding this comment.
@HumphreyYang just made a few suggestions.
My only other comment is that I can't remember a time whereI have ever written and read from a file in the same with statement before. I just wonder if this is overly complicating the example. I typically use a+ to append to an already established file. Thoughts?
| print(out) | ||
| ``` | ||
|
|
||
| We can also use `with` statement to contain operations on the file within a block for clarity and cleanness. |
There was a problem hiding this comment.
| We can also use `with` statement to contain operations on the file within a block for clarity and cleanness. | |
| We can also use a `with` statement (also known as a [context manager](https://realpython.com/python-with-statement/#the-with-statement-approach)) which enables you to group operations on the file within a block which improves the clarity of your code. |
There was a problem hiding this comment.
This sentence is too long and uses "which" twice.
Please remember that there must always be a comma before which. ("...within a block, which...")
"We can also use..." is the start of the previous sentence in the file so it's better not to reuse it.
| print(f'Line {i}: {line}') | ||
| ``` | ||
|
|
||
| Note that we used `a+` mode (standing for append+ mode) to allow appending new content at the end of the file and enable reading at the same time. |
There was a problem hiding this comment.
| Note that we used `a+` mode (standing for append+ mode) to allow appending new content at the end of the file and enable reading at the same time. | |
| ```{note} | |
| We used `a+` mode (standing for append+ mode) to allow appending new content at the end of the file and enable reading at the same time. | |
| ``` |
Hi @mmcky, Thanks for your detailed review. I was overcomplicating the example to show that there are other modes that are capable of doing operations other than |
thanks @HumphreyYang I would usually go down that path -- but I am a conservative programmer. I think though this would be good practice. |
|
Please let me know when to review @mmcky and @HumphreyYang . We can keep this addition to the lectures very simple and straightforward. Our only aim is to get people up to speed so that they can code well enough to handle subsequent econ lectures. |
|
Hi @mmcky, I have pushed a new version with the section on the pointer removed entirely since it will inevitably lead to complicated examples with many |
Thanks for the feedback @jstac, I will adjust the style to fit into this objective. |
| ```{code-cell} python3 | ||
| with open("newfile.txt", "r") as f: | ||
| file = f.readlines() | ||
| with open("output.txt", "w") as fo: |
There was a problem hiding this comment.
@HumphreyYang should block be indented to be in the open file context?
mmcky
left a comment
There was a problem hiding this comment.
thanks @HumphreyYang it is simpler.
Just had one question re: indentation
Thanks for picking that out @mmcky. I have added an indentation to the block. Should we invite John for review if it looks good to you? |
|
Hi @mmcky , it says above "This branch has not been deployed. No deployments." I can't see anything related to netlify. Am I missing something? |
|
The last The Netlify integration used to post a link each run but not sure why that behaviour changed. https://6383fedc52896355884dbec5--epic-agnesi-957267.netlify.app/intro.html That |
Ah, that's where I was going wrong. To be fair, it's a bad idea to have the downward scroll indicator hidden until you hover... Thanks @mmcky :-) |
|
Hi guys, I thought the point of the Perhaps this should be mentioned directly, so people can see the point. Also, I recommend that we start with the simple example f = open('newfile.txt', 'r')
out = f.read()
outand show how that changes when Then, after that, you could say: "Now suppose that we want to read input from one file and write output to another. Here's how we could do accomplish this task while correctly acquiring and returning resources to the operating system:" |
| fo.write(f'Line {i}: {line} \n') | ||
| ``` | ||
|
|
||
| ```{code-cell} python3 |
There was a problem hiding this comment.
Shouldn't we use with here, given that we just recommended it?
| fo.write(f'Line {i}: {line} \n') | ||
| ``` | ||
|
|
||
| ```{code-cell} python3 |
There was a problem hiding this comment.
Shouldn't we use with here, as above?
|
Great work @HumphreyYang . This is very nice. Just this comment to attend to:
How about "In fact, the recommended approach in modern Python is to use a |
Thanks for the feedback @jstac. It does sound much better. I have updated it accordingly. Many thanks. |
|
Thanks again @HumphreyYang , very nice examples. Merging. |
Hi @mmcky,
This PR addresses #247. Could you have a quick look at the additions when the preview is ready and merge if it looks good to you?
Many thanks in advance.