diff options
author | Zhongheng Liu on Homelab <z.liu@outlook.com.gr> | 2025-03-04 15:30:40 +0200 |
---|---|---|
committer | Zhongheng Liu on Homelab <z.liu@outlook.com.gr> | 2025-03-04 15:30:40 +0200 |
commit | 636fe6eaf079253e3a9b60e85d4d148141c5f1e2 (patch) | |
tree | dc284e26b8803ed55b5b8eaa99ac86ce816be5f3 | |
parent | a5e6039803ef03c451584ab5bd0fbf01166d10ac (diff) | |
download | cs-y13-636fe6eaf079253e3a9b60e85d4d148141c5f1e2.tar.gz cs-y13-636fe6eaf079253e3a9b60e85d4d148141c5f1e2.tar.bz2 cs-y13-636fe6eaf079253e3a9b60e85d4d148141c5f1e2.zip |
feat: funny pickle
-rw-r--r-- | notebooks/a_bit_of_a_pickle.ipynb | 120 | ||||
-rw-r--r-- | notebooks/file_handling.ipynb | 42 | ||||
-rw-r--r-- | notebooks/p41-mj17.ipynb | 30 | ||||
-rw-r--r-- | notebooks/test.py | 3 | ||||
-rw-r--r-- | thing.bin | bin | 0 -> 35 bytes |
5 files changed, 169 insertions, 26 deletions
diff --git a/notebooks/a_bit_of_a_pickle.ipynb b/notebooks/a_bit_of_a_pickle.ipynb new file mode 100644 index 0000000..e62e859 --- /dev/null +++ b/notebooks/a_bit_of_a_pickle.ipynb @@ -0,0 +1,120 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Pickle" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import pickle\n", + "class StudentObject:\n", + " def __init__(self):\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Using `pickle` to write multiple records\n", + "- input a student record and save it to a new serial file\n", + "- read a student record from that file\n", + "- extend your program to work for more than one record" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "John born on 2025-03-04 15:15:35.959676, full time is yes\n", + "John born on 2025-03-04 15:14:04.842286, full time is yes\n", + "Jake born on 2025-03-04 15:14:04.842378, full time is yes\n" + ] + } + ], + "source": [ + "import datetime\n", + "import pickle\n", + "DATA_FILENAME = \"student_data.dat\"\n", + "class Student:\n", + " def __init__(self, name, dob):\n", + " self.__Name = name\n", + " self.__DateOfBirth = dob\n", + " self.__FullTime = True\n", + " def GetName(self):\n", + " return self.__Name\n", + " def __str__(self):\n", + " return f\"{self.__Name} born on {self.__DateOfBirth}, full time is {'yes' if self.__FullTime else 'nah'}\"\n", + "def write_to_file(obj):\n", + " with open(DATA_FILENAME, 'ab+') as file:\n", + " pickle.dump(obj, file)\n", + " file.close()\n", + "def read_from_file():\n", + " with open(DATA_FILENAME, 'rb') as file:\n", + " s1 = pickle.load(file)\n", + " s2 = pickle.load(file)\n", + " file.close()\n", + " print(s1)\n", + " print(s2)\n", + "s1 = Student(\"John\", datetime.datetime.now())\n", + "s2 = Student(\"Jake\", datetime.datetime.now())\n", + "s3 = Student(\"Jaguar\", datetime.datetime.now())\n", + "s4 = Student(\"Jack\", datetime.datetime.now())\n", + "s5 = Student(\"Jacqueline\", datetime.datetime.now())\n", + "s6 = Student(\"Jiashuo\", datetime.datetime.now())\n", + "print(s1)\n", + "write_to_file(s1)\n", + "write_to_file(s2)\n", + "write_to_file(s3)\n", + "read_from_file()\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Activity 20M, with pickles" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.7" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/notebooks/file_handling.ipynb b/notebooks/file_handling.ipynb new file mode 100644 index 0000000..5a1aa0e --- /dev/null +++ b/notebooks/file_handling.ipynb @@ -0,0 +1,42 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Handling\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "my_file = open('filename.txt', 'w')\n", + "my_file.write(\"thingy\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.7" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/notebooks/p41-mj17.ipynb b/notebooks/p41-mj17.ipynb index b0b3bdd..0a42467 100644 --- a/notebooks/p41-mj17.ipynb +++ b/notebooks/p41-mj17.ipynb @@ -4,12 +4,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# May June 2017 Question 4" + "# May June 2017 Question 4\n", + "> As homework due for March 04, 2025" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -46,30 +47,7 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[None, None, None, None, None, None, None, None]\n", - "at: 0\n", - "at: 1\n", - "at: 2\n", - "at: 3\n", - "at: 4\n", - "at: 5\n", - "at: 6\n", - "[0] [None] -> (1)\n", - "[1] [None] -> (2)\n", - "[2] [None] -> (3)\n", - "[3] [None] -> (4)\n", - "[4] [None] -> (5)\n", - "[5] [None] -> (6)\n", - "[6] [None] -> (7)\n", - "[7] [None] -> (-1)\n" - ] - } - ], + "outputs": [], "source": [ "# Q4 c) iii)\n", "NULL_POINTER = -1\n", diff --git a/notebooks/test.py b/notebooks/test.py new file mode 100644 index 0000000..bdcc853 --- /dev/null +++ b/notebooks/test.py @@ -0,0 +1,3 @@ +import pickle +x = {"a": 1, "b": {"c": 2, }, } +pickle.dump(x, open("thing.bin", "wb+"))
\ No newline at end of file diff --git a/thing.bin b/thing.bin Binary files differnew file mode 100644 index 0000000..aa413c8 --- /dev/null +++ b/thing.bin |