summaryrefslogtreecommitdiff
path: root/notebooks/a_bit_of_a_pickle.ipynb
blob: e62e8597a5e2ee57c621f1118137a8f4c7b32f27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
}