Lightweight persistence for python variables. Example:: In [1]: l = ['hello',10,'world'] In [2]: %store l In [3]: exit (IPython session is closed and started again...) ville@badger:~$ ipython In [1]: l NameError: name
(self, parameter_s='')
| 76 | |
| 77 | @line_magic |
| 78 | def store(self, parameter_s=''): |
| 79 | """Lightweight persistence for python variables. |
| 80 | |
| 81 | Example:: |
| 82 | |
| 83 | In [1]: l = ['hello',10,'world'] |
| 84 | In [2]: %store l |
| 85 | In [3]: exit |
| 86 | |
| 87 | (IPython session is closed and started again...) |
| 88 | |
| 89 | ville@badger:~$ ipython |
| 90 | In [1]: l |
| 91 | NameError: name 'l' is not defined |
| 92 | In [2]: %store -r |
| 93 | In [3]: l |
| 94 | Out[3]: ['hello', 10, 'world'] |
| 95 | |
| 96 | Usage: |
| 97 | |
| 98 | * ``%store`` - Show list of all variables and their current |
| 99 | values |
| 100 | * ``%store spam bar`` - Store the *current* value of the variables spam |
| 101 | and bar to disk |
| 102 | * ``%store -d spam`` - Remove the variable and its value from storage |
| 103 | * ``%store -z`` - Remove all variables from storage |
| 104 | * ``%store -r`` - Refresh all variables, aliases and directory history |
| 105 | from store (overwrite current vals) |
| 106 | * ``%store -r spam bar`` - Refresh specified variables and aliases from store |
| 107 | (delete current val) |
| 108 | * ``%store foo >a.txt`` - Store value of foo to new file a.txt |
| 109 | * ``%store foo >>a.txt`` - Append value of foo to file a.txt |
| 110 | |
| 111 | It should be noted that if you change the value of a variable, you |
| 112 | need to %store it again if you want to persist the new value. |
| 113 | |
| 114 | Note also that the variables will need to be pickleable; most basic |
| 115 | python types can be safely %store'd. |
| 116 | |
| 117 | Also aliases can be %store'd across sessions. |
| 118 | To remove an alias from the storage, use the %unalias magic. |
| 119 | """ |
| 120 | |
| 121 | opts,argsl = self.parse_options(parameter_s,'drz',mode='string') |
| 122 | args = argsl.split() |
| 123 | ip = self.shell |
| 124 | db = ip.db |
| 125 | # delete |
| 126 | if 'd' in opts: |
| 127 | try: |
| 128 | todel = args[0] |
| 129 | except IndexError: |
| 130 | raise UsageError('You must provide the variable to forget') |
| 131 | else: |
| 132 | try: |
| 133 | del db['autorestore/' + todel] |
| 134 | except: |
| 135 | raise UsageError("Can't delete variable '%s'" % todel) |
nothing calls this directly
no test coverage detected