{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "In lecture 9 of mcs 320, we save data and load data from file." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Saving Data as Plain Text" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.1415926535897932385 has type \n" ] } ], "source": [ "x = pi.n(digits=20)\n", "print(x, 'has type', type(x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will write the string representation of x to file." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.1415926535897932385 has type \n" ] } ], "source": [ "sx = str(x)\n", "print(sx, 'has type', type(sx))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "file = open('mydata.txt', 'w')\n", "file.write(sx)\n", "file.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can check what is in the listing of the current directory." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['.ipynb_checkpoints',\n", " '.READ_ME.un~',\n", " 'mcs320lec09.ipynb',\n", " 'mcs320lec09s19.ipynb',\n", " 'mydata.txt',\n", " 'pickledresult.txt',\n", " 'READ_ME',\n", " 'READ_ME~',\n", " 'sageobject.sobj']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import os\n", "os.listdir('.')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "x" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reset() # clear everything\n", "x" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.1415926535897932385 has type \n" ] } ], "source": [ "file = open('mydata.txt', 'r')\n", "line = file.readline()\n", "print(line, 'has type', type(line))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we have the string, we have to convert this back into a proper SageMath object.\n", "Python question: what is the opposite of ``str()``?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We could cast our number to a float." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.141592653589793" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(line)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.14159265358979" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "RR(line)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.141592653589793 has type \n" ] } ], "source": [ "ex = eval(line)\n", "print(ex, 'has type', type(ex))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In SageMath, we have to call the preparser on the string, before evaluation." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "RealNumber('3.1415926535897932385') has type \n" ] } ], "source": [ "px = preparse(line)\n", "print(px, 'has type', type(px))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.141592653589793239 has type \n" ] } ], "source": [ "result = eval(px)\n", "print(result, 'has type', type(result))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2. Saving Objects with save() and Retrieving with load()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.141592653589793239 has type \n" ] } ], "source": [ "print(result, 'has type', type(result))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "result.save('sageobject')" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['.ipynb_checkpoints',\n", " '.READ_ME.un~',\n", " 'mcs320lec09.ipynb',\n", " 'mcs320lec09s19.ipynb',\n", " 'mydata.txt',\n", " 'pickledresult.txt',\n", " 'READ_ME',\n", " 'READ_ME~',\n", " 'sageobject.sobj']" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.listdir('.')" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "result" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# reset('result')\n", "# result\n", "result = var('result')\n", "result" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.141592653589793239 has type \n" ] } ], "source": [ "result = load('sageobject')\n", "print(result, 'has type', type(result))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3. Pickling Python Objects" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'\\x80\\x03csage.rings.real_mpfr\\n__create__RealNumber_version0\\nq\\x00csage.rings.real_mpfr\\n__create__RealField_version0\\nq\\x01KC\\x89X\\x04\\x00\\x00\\x00RNDNq\\x02\\x87q\\x03Rq\\x04X\\x12\\x00\\x00\\x003.4gvml245kc4d80@0q\\x05K \\x87q\\x06Rq\\x07.'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pickle\n", "pr = pickle.dumps(result)\n", "pr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The pickled object contains the type information.\n", "We can write this to a file, as a string." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "file = open('pickledresult.txt', 'w')\n", "file.write(str(pr))\n", "file.close()" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['.ipynb_checkpoints',\n", " '.READ_ME.un~',\n", " 'mcs320lec09.ipynb',\n", " 'mcs320lec09s19.ipynb',\n", " 'mydata.txt',\n", " 'pickledresult.txt',\n", " 'READ_ME',\n", " 'READ_ME~',\n", " 'sageobject.sobj']" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.listdir('.')" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "result" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = var('result')\n", "result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With ``del`` (a pure Python command), we deleted the object result.\n", "Unlike the ``result = var('result')`` the name result does not exist.\n", "Now we will retrieve the pickled data." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[\"b'\\\\x80\\\\x03csage.rings.real_mpfr\\\\n__create__RealNumber_version0\\\\nq\\\\x00csage.rings.real_mpfr\\\\n__create__RealField_version0\\\\nq\\\\x01KC\\\\x89X\\\\x04\\\\x00\\\\x00\\\\x00RNDNq\\\\x02\\\\x87q\\\\x03Rq\\\\x04X\\\\x12\\\\x00\\\\x00\\\\x003.4gvml245kc4d80@0q\\\\x05K \\\\x87q\\\\x06Rq\\\\x07.'\"] has type \n" ] } ], "source": [ "file = open('pickledresult.txt', 'r')\n", "lines = file.readlines()\n", "print(lines, 'has type', type(lines))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: how do we turn a list of strings into one big string?" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"b'\\\\x80\\\\x03csage.rings.real_mpfr\\\\n__create__RealNumber_version0\\\\nq\\\\x00csage.rings.real_mpfr\\\\n__create__RealField_version0\\\\nq\\\\x01KC\\\\x89X\\\\x04\\\\x00\\\\x00\\\\x00RNDNq\\\\x02\\\\x87q\\\\x03Rq\\\\x04X\\\\x12\\\\x00\\\\x00\\\\x003.4gvml245kc4d80@0q\\\\x05K \\\\x87q\\\\x06Rq\\\\x07.'\"" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "joinedpr = ''.join(lines)\n", "joinedpr" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'\\x80\\x03csage.rings.real_mpfr\\n__create__RealNumber_version0\\nq\\x00csage.rings.real_mpfr\\n__create__RealField_version0\\nq\\x01KC\\x89X\\x04\\x00\\x00\\x00RNDNq\\x02\\x87q\\x03Rq\\x04X\\x12\\x00\\x00\\x003.4gvml245kc4d80@0q\\x05K \\x87q\\x06Rq\\x07.' has type \n" ] } ], "source": [ "bytejoinedpr = eval(joinedpr)\n", "print(bytejoinedpr, 'has type', type(bytejoinedpr))" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.141592653589793239 has type \n" ] } ], "source": [ "r = pickle.loads(bytejoinedpr)\n", "print(r, 'has type', type(r))" ] } ], "metadata": { "kernelspec": { "display_name": "SageMath 10.3", "language": "sage", "name": "sagemath" }, "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.10" } }, "nbformat": 4, "nbformat_minor": 2 }