{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "3c71b5b1-46f1-4029-bae2-f5761194e5af", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Main.My" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "module My\n", "\n", "\"\"\"\n", " savevar(fn, x)\n", "\n", "saves the value of `x` to the file `fn`, where `fn` is the filename string of the file.\n", "\"\"\"\n", "savevar(fn, x) = write(fn, string(x))\n", "\n", "\"\"\"\n", " loadvar(fn)\n", "\n", "loads the file `fn` (the filename string of the file) and `Meta.parse |> eval`.\n", "\"\"\"\n", "loadvar(fn) = read(fn, String) |> Meta.parse |> eval\n", "\n", "\"\"\"\n", " dir_savevar[]\n", "\n", "is the default directory to which `@savevar` saves the values of variables.\n", "\"\"\"\n", "const dir_savevar = Ref(\".\")\n", "\n", "\"\"\"\n", " fn_savevar(x::Symbol)\n", "\n", "is the filename string to which `@savevar` saves the value of a variable.\n", "\"\"\"\n", "fn_savevar(x::Symbol) = joinpath(dir_savevar[], string(x) * \".txt\")\n", "\n", "\"\"\"\n", " @savevar(args...)\n", "\n", "saves the variables in args to the corresponding textfiles.\n", "\n", "Example: `@savevar A B C` saves the variables `A`, `B`, `C` to textfiles. \n", "\"\"\"\n", "macro savevar(args...)\n", " A = [:(savevar($(fn_savevar(x)), $(esc(x)))) for x in args]\n", " quote $(A...); nothing end\n", "end\n", "\n", "\"\"\"\n", " @loadvar(args...)\n", "\n", "loads the values from the textfiles corresponding to `args`.\n", "If `length(args)` is greater than 1, then it returns the tuple of the values.\n", "\n", "Example: `a, b, c = @loadvar A B C` loads \n", "the values of `A`, `B`, `C` in textfiles to the variables `a`, `b`, `c`.\n", "\"\"\"\n", "macro loadvar(args...)\n", " if length(args) == 1\n", " x = args[1]\n", " :(loadvar($(fn_savevar(x))))\n", " else\n", " A = [:(loadvar($(fn_savevar(x)))) for x in args]\n", " :(($(A...),))\n", " end\n", "end\n", "\n", "end" ] }, { "cell_type": "code", "execution_count": 2, "id": "74122513-2cdd-452c-b3b0-c6a8bce8bed4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"tmp\"" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using .My: dir_savevar, @savevar, @loadvar\n", "dir_savevar[] = \"tmp\"" ] }, { "cell_type": "code", "execution_count": 3, "id": "9cba9988-3492-4f47-b3d7-79e8a16820a6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "search:\n", "\n" ] }, { "data": { "text/latex": [ "\\begin{verbatim}\n", "dir_savevar[]\n", "\\end{verbatim}\n", "is the default directory to which \\texttt{@savevar} saves the values of variables.\n", "\n" ], "text/markdown": [ "```\n", "dir_savevar[]\n", "```\n", "\n", "is the default directory to which `@savevar` saves the values of variables.\n" ], "text/plain": [ "\u001b[36m dir_savevar[]\u001b[39m\n", "\n", " is the default directory to which \u001b[36m@savevar\u001b[39m saves the values of variables." ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "?dir_savevar" ] }, { "cell_type": "code", "execution_count": 4, "id": "d8033deb-6ce4-4177-b48e-a08c0b266c7f", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "\\begin{verbatim}\n", "@savevar(args...)\n", "\\end{verbatim}\n", "saves the variables in args to the corresponding textfiles.\n", "\n", "Example: \\texttt{@savevar A B C} saves the variables \\texttt{A}, \\texttt{B}, \\texttt{C} to textfiles. \n", "\n" ], "text/markdown": [ "```\n", "@savevar(args...)\n", "```\n", "\n", "saves the variables in args to the corresponding textfiles.\n", "\n", "Example: `@savevar A B C` saves the variables `A`, `B`, `C` to textfiles. \n" ], "text/plain": [ "\u001b[36m @savevar(args...)\u001b[39m\n", "\n", " saves the variables in args to the corresponding textfiles.\n", "\n", " Example: \u001b[36m@savevar A B C\u001b[39m saves the variables \u001b[36mA\u001b[39m, \u001b[36mB\u001b[39m, \u001b[36mC\u001b[39m to textfiles." ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "?@savevar" ] }, { "cell_type": "code", "execution_count": 5, "id": "1b610bb4-303d-44fb-9b96-8059a1402119", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "\\begin{verbatim}\n", "@loadvar(args...)\n", "\\end{verbatim}\n", "loads the values from the textfiles corresponding to \\texttt{args}. If \\texttt{length(args)} is greater than 1, then it returns the tuple of the values.\n", "\n", "Example: \\texttt{a, b, c = @loadvar A B C} loads the values of \\texttt{A}, \\texttt{B}, \\texttt{C} in textfiles to the variables \\texttt{a}, \\texttt{b}, \\texttt{c}.\n", "\n" ], "text/markdown": [ "```\n", "@loadvar(args...)\n", "```\n", "\n", "loads the values from the textfiles corresponding to `args`. If `length(args)` is greater than 1, then it returns the tuple of the values.\n", "\n", "Example: `a, b, c = @loadvar A B C` loads the values of `A`, `B`, `C` in textfiles to the variables `a`, `b`, `c`.\n" ], "text/plain": [ "\u001b[36m @loadvar(args...)\u001b[39m\n", "\n", " loads the values from the textfiles corresponding to \u001b[36margs\u001b[39m. If \u001b[36mlength(args)\u001b[39m\n", " is greater than 1, then it returns the tuple of the values.\n", "\n", " Example: \u001b[36ma, b, c = @loadvar A B C\u001b[39m loads the values of \u001b[36mA\u001b[39m, \u001b[36mB\u001b[39m, \u001b[36mC\u001b[39m in textfiles\n", " to the variables \u001b[36ma\u001b[39m, \u001b[36mb\u001b[39m, \u001b[36mc\u001b[39m." ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "?@loadvar" ] }, { "cell_type": "code", "execution_count": 6, "id": "5d3267a7-1a3a-4ff1-9761-b7134560fac6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MersenneTwister(4649373)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using Random; Random.seed!(4649373)" ] }, { "cell_type": "code", "execution_count": 7, "id": "e537cfc2-f1a2-4621-87a9-34ee2e987b6b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×3×2 Array{ComplexF64, 3}:\n", "[:, :, 1] =\n", " 0.357345-1.2478im 0.682675-0.19939im -0.828604-0.130312im\n", " 0.999778-0.128819im 0.356639+0.0984794im -0.249303+0.0792775im\n", " 0.646351+0.287488im -1.63661-1.1631im 0.0147973-0.0963827im\n", " -0.838917+0.305942im -0.276665-0.339579im 1.02587-0.00997503im\n", "\n", "[:, :, 2] =\n", " -0.0701421+1.07701im 0.229101+0.35718im -1.06954+0.966731im\n", " 0.212719+0.0450435im -0.212064-1.05741im -0.100229+0.0276602im\n", " 1.22601-0.671355im 0.507781+0.146455im 0.131755+1.1808im\n", " 0.20559+0.687265im -0.407012-1.16322im -0.130588-0.832639im" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = randn(ComplexF64, 4, 3, 2)" ] }, { "cell_type": "code", "execution_count": 8, "id": "bda031fa-c871-4d1f-b410-c087d274a3eb", "metadata": {}, "outputs": [], "source": [ "@savevar A" ] }, { "cell_type": "code", "execution_count": 9, "id": "45421e27-b39a-4da3-a913-4bb8b8622c4f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"[0.3573446150049466 - 1.247796689524791im 0.6826751518535276 - 0.19938984132658544im -0.828604404251341 - 0.13031243958002936im; 0.9997775125877176 - 0.1288185084503161im 0.3566393536097865 + 0.09847938776643947im -0.24930302594101603 + 0.07927746587437946im; 0.646350739\" ⋯ 499 bytes ⋯ \"02270299698im; 1.226008377537175 - 0.6713548316258817im 0.5077812548164254 + 0.14645450926431483im 0.1317552128204387 + 1.1807955920537025im; 0.2055903575599693 + 0.6872649191653237im -0.4070115367381583 - 1.1632213017570572im -0.13058807817013474 - 0.832638913474766im]\"" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "read(\"tmp/A.txt\", String)" ] }, { "cell_type": "code", "execution_count": 10, "id": "a15bace7-d717-46fa-aee4-265654e2b0ea", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×3×2 Array{ComplexF64, 3}:\n", "[:, :, 1] =\n", " 0.357345-1.2478im 0.682675-0.19939im -0.828604-0.130312im\n", " 0.999778-0.128819im 0.356639+0.0984794im -0.249303+0.0792775im\n", " 0.646351+0.287488im -1.63661-1.1631im 0.0147973-0.0963827im\n", " -0.838917+0.305942im -0.276665-0.339579im 1.02587-0.00997503im\n", "\n", "[:, :, 2] =\n", " -0.0701421+1.07701im 0.229101+0.35718im -1.06954+0.966731im\n", " 0.212719+0.0450435im -0.212064-1.05741im -0.100229+0.0276602im\n", " 1.22601-0.671355im 0.507781+0.146455im 0.131755+1.1808im\n", " 0.20559+0.687265im -0.407012-1.16322im -0.130588-0.832639im" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A_load = @loadvar A" ] }, { "cell_type": "code", "execution_count": 11, "id": "42f54459-a5bc-45b0-8f64-6a218b4219ee", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A_load == A" ] }, { "cell_type": "code", "execution_count": 12, "id": "a4393e1d-c5d4-41bf-b443-4cb53cc9fd5f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Vector{String}:\n", " \"Foo\"\n", " \"Bar\"\n", " \"Baz\"" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B = [\"Foo\", \"Bar\", \"Baz\"]" ] }, { "cell_type": "code", "execution_count": 13, "id": "cefee068-405e-4a36-a176-520f08e9bb65", "metadata": {}, "outputs": [], "source": [ "@savevar B" ] }, { "cell_type": "code", "execution_count": 14, "id": "67b4a505-513b-4ceb-b239-3f43f8e5d5c8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"[\\\"Foo\\\", \\\"Bar\\\", \\\"Baz\\\"]\"" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "read(\"tmp/B.txt\", String)" ] }, { "cell_type": "code", "execution_count": 15, "id": "695629dc-b95c-44c4-b5da-39ccb20bc90e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Vector{String}:\n", " \"Foo\"\n", " \"Bar\"\n", " \"Baz\"" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B_load = @loadvar B" ] }, { "cell_type": "code", "execution_count": 16, "id": "e9fd6e4b-5a9a-42ef-9353-64cfa5b417af", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B_load == B" ] }, { "cell_type": "code", "execution_count": 17, "id": "2079c4b2-fbea-4eab-ac6f-83a23541c0c7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Dict{Symbol, Array} with 2 entries:\n", " :A => [0.357345-1.2478im 0.682675-0.19939im -0.828604-0.130312im; 0.999778-0.…\n", " :B => [\"Foo\", \"Bar\", \"Baz\"]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "D = Dict(:A => A, :B => B)" ] }, { "cell_type": "code", "execution_count": 18, "id": "9d665b66-449f-4cf0-8aa8-c24f6c8e199d", "metadata": {}, "outputs": [], "source": [ "@savevar D" ] }, { "cell_type": "code", "execution_count": 19, "id": "09813a8e-f3f9-4539-ae5f-cb1b26dde64d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Dict{Symbol, Array}(:A => [0.3573446150049466 - 1.247796689524791im 0.6826751518535276 - 0.19938984132658544im -0.828604404251341 - 0.13031243958002936im; 0.9997775125877176 - 0.1288185084503161im 0.3566393536097865 + 0.09847938776643947im -0.24930302594101603 + 0.079277\" ⋯ 555 bytes ⋯ \"75 - 0.6713548316258817im 0.5077812548164254 + 0.14645450926431483im 0.1317552128204387 + 1.1807955920537025im; 0.2055903575599693 + 0.6872649191653237im -0.4070115367381583 - 1.1632213017570572im -0.13058807817013474 - 0.832638913474766im], :B => [\\\"Foo\\\", \\\"Bar\\\", \\\"Baz\\\"])\"" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "read(\"tmp/D.txt\", String)" ] }, { "cell_type": "code", "execution_count": 20, "id": "8dd57510-df29-4ac9-9eab-0504b03630cc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Dict{Symbol, Array} with 2 entries:\n", " :A => [0.357345-1.2478im 0.682675-0.19939im -0.828604-0.130312im; 0.999778-0.…\n", " :B => [\"Foo\", \"Bar\", \"Baz\"]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "D_load = @loadvar D" ] }, { "cell_type": "code", "execution_count": 21, "id": "6a4bbec8-6b46-426c-88a2-1ad3ccafb99e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "D_load == D" ] }, { "cell_type": "code", "execution_count": 22, "id": "465c8021-69c7-4018-a7cf-b1c073fb8de0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Main.O" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "module O\n", "struct Foo{A, B}\n", " a::A\n", " b::B\n", "end\n", "end" ] }, { "cell_type": "code", "execution_count": 23, "id": "53c54353-cab9-4b61-a7fc-f6b5ae259236", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Main.O.Foo{Array{ComplexF64, 3}, Vector{String}}([0.3573446150049466 - 1.247796689524791im 0.6826751518535276 - 0.19938984132658544im -0.828604404251341 - 0.13031243958002936im; 0.9997775125877176 - 0.1288185084503161im 0.3566393536097865 + 0.09847938776643947im -0.24930302594101603 + 0.07927746587437946im; 0.6463507394027657 + 0.2874875698910842im -1.6366131682001521 - 1.1630981589087876im 0.01479727941198744 - 0.09638269986747496im; -0.8389169497092693 + 0.3059416490070709im -0.276664821700539 - 0.33957890029521076im 1.0258672083835658 - 0.00997502769507162im;;; -0.07014210671299528 + 1.077013678925162im 0.22910085513407732 + 0.3571799374969964im -1.0695368505074354 + 0.9667307185166448im; 0.21271939342846552 + 0.0450434630802757im -0.2120637210443527 - 1.0574146943973246im -0.10022936815590866 + 0.027660202270299698im; 1.226008377537175 - 0.6713548316258817im 0.5077812548164254 + 0.14645450926431483im 0.1317552128204387 + 1.1807955920537025im; 0.2055903575599693 + 0.6872649191653237im -0.4070115367381583 - 1.1632213017570572im -0.13058807817013474 - 0.832638913474766im], [\"Foo\", \"Bar\", \"Baz\"])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "foo = O.Foo(A, B)" ] }, { "cell_type": "code", "execution_count": 24, "id": "67f7aff5-4d45-4eb1-8455-3d2ddd4fa0fb", "metadata": {}, "outputs": [], "source": [ "@savevar foo" ] }, { "cell_type": "code", "execution_count": 25, "id": "9f287236-8dde-4436-90f2-68a85527c086", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Main.O.Foo{Array{ComplexF64, 3}, Vector{String}}([0.3573446150049466 - 1.247796689524791im 0.6826751518535276 - 0.19938984132658544im -0.828604404251341 - 0.13031243958002936im; 0.9997775125877176 - 0.1288185084503161im 0.3566393536097865 + 0.09847938776643947im -0.24930\" ⋯ 572 bytes ⋯ \"77537175 - 0.6713548316258817im 0.5077812548164254 + 0.14645450926431483im 0.1317552128204387 + 1.1807955920537025im; 0.2055903575599693 + 0.6872649191653237im -0.4070115367381583 - 1.1632213017570572im -0.13058807817013474 - 0.832638913474766im], [\\\"Foo\\\", \\\"Bar\\\", \\\"Baz\\\"])\"" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "read(\"tmp/foo.txt\", String)" ] }, { "cell_type": "code", "execution_count": 26, "id": "d7aef901-a360-491a-8399-18fe242b28cb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Main.O.Foo{Array{ComplexF64, 3}, Vector{String}}([0.3573446150049466 - 1.247796689524791im 0.6826751518535276 - 0.19938984132658544im -0.828604404251341 - 0.13031243958002936im; 0.9997775125877176 - 0.1288185084503161im 0.3566393536097865 + 0.09847938776643947im -0.24930302594101603 + 0.07927746587437946im; 0.6463507394027657 + 0.2874875698910842im -1.6366131682001521 - 1.1630981589087876im 0.01479727941198744 - 0.09638269986747496im; -0.8389169497092693 + 0.3059416490070709im -0.276664821700539 - 0.33957890029521076im 1.0258672083835658 - 0.00997502769507162im;;; -0.07014210671299528 + 1.077013678925162im 0.22910085513407732 + 0.3571799374969964im -1.0695368505074354 + 0.9667307185166448im; 0.21271939342846552 + 0.0450434630802757im -0.2120637210443527 - 1.0574146943973246im -0.10022936815590866 + 0.027660202270299698im; 1.226008377537175 - 0.6713548316258817im 0.5077812548164254 + 0.14645450926431483im 0.1317552128204387 + 1.1807955920537025im; 0.2055903575599693 + 0.6872649191653237im -0.4070115367381583 - 1.1632213017570572im -0.13058807817013474 - 0.832638913474766im], [\"Foo\", \"Bar\", \"Baz\"])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "foo_load = @loadvar foo" ] }, { "cell_type": "code", "execution_count": 27, "id": "37d1cb3a-1f4e-4126-b959-c56d3c1c193c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(foo_load.a, foo_load.b) == (foo.a, foo.b)" ] }, { "cell_type": "code", "execution_count": 28, "id": "00f838ed-2ffb-4d51-9bbe-4ca3487d47cd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(0.25809857024548255, -0.6921471091207019, 0.3180664127334103)" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x, y, z = randn(3)\n", "x, y, z" ] }, { "cell_type": "code", "execution_count": 29, "id": "91ebe1fb-3020-4073-9b46-ebe0ab5ea607", "metadata": {}, "outputs": [], "source": [ "@savevar x y z" ] }, { "cell_type": "code", "execution_count": 30, "id": "429be5bb-bb2b-4d39-bef5-d5a9ba924ef2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(0.25809857024548255, -0.6921471091207019, 0.3180664127334103)" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X, Y, Z = @loadvar x y z\n", "X, Y, Z" ] }, { "cell_type": "code", "execution_count": 31, "id": "dd5193f9-57db-466f-8ec9-36904971466d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(X, Y, Z) == (x, y, z)" ] }, { "cell_type": "code", "execution_count": null, "id": "9ce11fed-d02a-4ef5-8bd7-a1f295262c57", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "jupytext": { "formats": "ipynb,md" }, "kernelspec": { "display_name": "Julia 1.7.0-DEV", "language": "julia", "name": "julia-1.7" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.7.0" } }, "nbformat": 4, "nbformat_minor": 5 }