Skip to content

Conversation

rsokl
Copy link
Owner

@rsokl rsokl commented Jun 23, 2018

  • add pickle discussion
  • reorganize and clean up some content
  • remove context managers for numpy examples
Copy link
Collaborator

@davidmascharka davidmascharka left a comment

Choose a reason for hiding this comment

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

Looks good! Just a couple minor wording tweaks recommended.

"source": [
"# Working with Files\n",
"This section will discuss the best practices for writing Python code that involves reading from and writing to files. We will learn about the built-in `pathlib.Path` object, which will help to ensure that the code that we write is portable across operating systems (OS) (i.e. Windows vs MacOS vs Linux). We will also be introduced to a so-called context manager, which will permit us to read-from and write-to a file safely; by \"safely\" we mean that we will be assured that any file that we open will eventually be closed properly, so that it will not be corrupted even in the event that our code hits an error."
"This section will discuss the best practices for writing Python code that involves reading from and writing to files. We will learn about the built-in `pathlib.Path` object, which will help to ensure that the code that we write is portable across operating systems (OS) (e.g. Windows, MacOS, Linux). We will also be introduced to a *context manager*, `open`, which will permit us to read-from and write-to a file safely; by \"safely\" we mean that we will be assured that any file that we open will eventually be closed properly, so that it will not be corrupted even in the event that our code hits an error. Lastly, we will briefly encounter the `pickle` module which allows you to save (or \"pickle\") and load Python objects to and from your computer's file system. "
Copy link
Collaborator

Choose a reason for hiding this comment

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

For consistent voice, this should say "which allows us to save (or "pickle") and load Python objects to and from our computer's file system"

"path_to_file = Path(\".\") / \"file1.txt\"\n",
"# we will write to the file named \"file1.txt\", located \n",
"# in the present directory\n",
"path_to_file = Path(\"file1.txt\")\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah...I had originally wondered why you wanted to do ./file1.txt but I figured you had your reasons 😛

"```\n",
"\n",
"Now read in each line of the file and append that line to the list `out`, but *only if that line starts with the letter 'A'*:\n",
"Now let's read in each line of the file and append that line to the list `out`, but *only if that line starts with the letter 'A'* (just to make things a little bit more involved):\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe say "append them to the list out..." instead? Read in each line and append that line sounds weird.

"with open(\"grades.pkl\", mode=\"wb\") as opened_file:\n",
" pickle.dump(grades, opened_file)\n",
"```\n",
"`pickle.dump` creates a serialized representation of our dictionary, which is then written to our opened file via the file object that we supplied. Note that we open the file in write-binary mode as we are writing binary data and not text data that first needs to be encoded to binary data. Also note that we use the \".pkl\" suffix to indicate that the file is binary data that was written using Python's pickle protocol. Using this suffix is not necessary but is good practice.\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you need to explain what a serialized representation is?

@rsokl rsokl merged commit a4f18be into master Jun 26, 2018
@davidmascharka davidmascharka deleted the pickle branch September 2, 2018 22:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants