In [1]:
import RFigure,os
filepath_testfile  = "Test.rfig3"
if "tests" in os.listdir():
    filepath_testfile = os.path.join("tests",filepath_testfile)
assert os.path.exists(filepath_testfile)

In [2]:
%reload_ext RFigure.RFigureMagics

In [3]:
import numpy as np
X = np.arange(0,10,.1)
Y = np.cos(X)
c = "This is a comment"
d = dict(X=X,Y=1/X) # another variable dictionary


/home/dessalles/Programmation/Python/Project_RFigure/noGithub/myenv/lib/python3.6/site-packages/ipykernel_launcher.py:5: RuntimeWarning: divide by zero encountered in true_divide
  """

Test of rfig_save


In [4]:
%%rfig_save -h
pass


usage: %%rfig_save [--help] [--format_name] [-d D] [-c C]
                   [--fig_type FIG_TYPE]
                   [filepath]

Will save a RFigure, whose instructions are the code written in the remaining of the cell.

positional arguments:
  filepath              Path of the file.

optional arguments:
  --help, -h            show this help message and exit
  --format_name, -fn    Format the name of the file as Figure_YYYYMMDD_foo
                        where YYYYMMDD stands for the date. `foo` will be the
                        file names. If the file name is already under this
                        format, do notiong.
  -d D                  Dictionary of the locals in the rfigure file. If not
                        specified, guess from the instructions.
  -c C                  Comments associated to the file
  --fig_type FIG_TYPE, -ft FIG_TYPE
                        extension of the figure, should be in ['eps', 'pdf',
                        'png']

Examples (in IPython/Jupyter):

In[1]:
> import numpy as np
> a = np.arange(0,10,.1)
> b = np.cos(a)
> comment = "A comment"
> diction = {'a':a,'b':1/a}

In[2]:
> %%rfig_save Test
> # search the variables in the instructions, no comment and save in pdf
> plt.plot(a,b)

In[3]:
> %%rfig_save -c comment Test
> # search the variables in the instructions, with a comment and save in pdf
> plt.plot(a,b)

In[4]:
> %%rfig_save --fig_type png
> # search the variables in the instructions, no comment and save in png
> plt.plot(a,b)

In[5]:
> %%rfig_save -d diction Test
> # specify other variables, no comment, save in pdf
> plt.plot(a,b)

In[5]:
> %%rfig_save --format_name Test
> # search the variables in the instructions, format the filename
> plt.plot(a,b)
Out[4]:
True

In [5]:
%%rfig_save Test.rfig3
# search the variables in the instructions, no comment and save in pdf
plt.plot(X,Y)


We determined the RFigure variables to be: `X`, `Y`
Pickle success
========== END SAVE =========

In [6]:
%%rfig_save -c c Test.rfig3
# search the variables in the instructions, put the comment of the variable `c`
plt.plot(X,Y)


We determined the RFigure variables to be: `X`, `Y`
Pickle success
========== END SAVE =========

In [7]:
%%rfig_save -c c -d d Test.rfig3
# search the variables in the instructions, put the comment of the variable `c`
plt.plot(X,Y)


Pickle success
========== END SAVE =========

In [8]:
%%rfig_save -c "'This is another comment'" Test.rfig3
# search the variables in the instructions, put the comment of the variable `c`
plt.plot(X,Y)


We determined the RFigure variables to be: `X`, `Y`
Pickle success
========== END SAVE =========

In [9]:
%%rfig_save -c "'This is another comment'" -fn Test.rfig3
# search the variables in the instructions, put the comment of the variable `c`
plt.plot(X,Y)


We determined the RFigure variables to be: `X`, `Y`
Pickle success
========== END SAVE =========

In [10]:
# Assert the files with formated name and removes it
import os
from RFigure.RFigureMisc import RDateDisplay

f =  "Figure_%s_Test"%RDateDisplay.cur_date()
print(f)
assert os.path.exists(f+".pdf")
os.remove(f+".pdf")
assert os.path.exists(f+".rfig3")
os.remove(f+".rfig3")


Figure_20190122_Test

In [11]:
%%rfig_save -c "'This is another comment'" -ft eps Test.rfig3
# search the variables in the instructions, put the comment of the variable `c`
plt.plot(X,Y)


We determined the RFigure variables to be: `X`, `Y`
Pickle success
========== END SAVE =========

In [12]:
# Assert the eps files has been created and remove it
assert os.path.exists("Test.eps")
os.remove("Test.eps")

Test of rfig_load


In [13]:
%rfig_load -h


usage: %%rfig_load [--help] [-d D] [-i I] [-c C] [filepath]

Will load a RFigure in the Jupyter notebook.

positional arguments:
  filepath    Path of the file to open.

optional arguments:
  --help, -h  show this help message and exit
  -d D        the variable name of the dictionary in which will be stored the
              variables of the RFigure. If none is given, import in the
              notebook locals.
  -i I        the variable name of the string in which will be stored the
              instructions of the RFigure. If none is given, create a new cell
              filled with the instructions. Also checks if the magic `%pylab`
              has been executed. If not, it adds the command `%pylab` at the
              begining of the instructions
  -c C        the variable name of the string in which will be stored the
              commentaries of the RFigure.
Out[13]:
True

In [14]:
%rfig_load -i iii -c ccc Test.rfig3


Success instructions

In [15]:
# Assert that the comments and the instructions have been upload
assert ccc == 'This is another comment'
assert iii == '%pylab\n# search the variables in the instructions, put the comment of the variable `c`\nplt.plot(X,Y)'

Test of rfig_list_var


In [16]:
%%rfig_list_var -h
pass


usage: %%rfig_list_var [--help] [dict_variable]

Detects the variables in the code of the cell.

positional arguments:
  dict_variable  the variable name of the dictionary in which will be stored
                 the variables detected in the cell. If none is given, only
                 prints its keys.

optional arguments:
  --help, -h     show this help message and exit

Examples (in IPython/Jupyter):

In[1]:
> a = np.arange(0,10,.1)
> b = np.cos(a)

In[2]:
> %list_var
> plot(a,b)
We determined the RFigure variables to be: `a`, `b`

In[3]:
> %list_var a_dict
> plot(a,b)
We determined the RFigure variables to be: `a`, `b`

In[4]:
> print(a_dict.keys())
dict_keys(['a','b'])
Out[16]:
True

In [17]:
%%rfig_list_var
plot(X,Y)


We determined the RFigure variables to be: `X`, `Y`

In [18]:
%%rfig_list_var a_dict
plot(X,Y)


We determined the RFigure variables to be: `X`, `Y`
args.dict_variable[0] a_dict

In [19]:
assert set(a_dict.keys())=={'X','Y'}

In [1]:



---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-6ee43eeb285b> in <module>
----> 1 get_ipython().run_line_magic('reload_ext', 'RFigure.RfigureMagics')

~/.local/lib/python3.6/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
   2283                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2284             with self.builtin_trap:
-> 2285                 result = fn(*args,**kwargs)
   2286             return result
   2287 

<decorator-gen-67> in reload_ext(self, module_str)

~/.local/lib/python3.6/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

~/.local/lib/python3.6/site-packages/IPython/core/magics/extension.py in reload_ext(self, module_str)
     61         if not module_str:
     62             raise UsageError('Missing module name.')
---> 63         self.shell.extension_manager.reload_extension(module_str)

~/.local/lib/python3.6/site-packages/IPython/core/extensions.py in reload_extension(self, module_str)
    128                 self.loaded.add(module_str)
    129         else:
--> 130             self.load_extension(module_str)
    131 
    132     def _call_load_ipython_extension(self, mod):

~/.local/lib/python3.6/site-packages/IPython/core/extensions.py in load_extension(self, module_str)
     78             if module_str not in sys.modules:
     79                 with prepended_to_syspath(self.ipython_extension_dir):
---> 80                     mod = import_module(module_str)
     81                     if mod.__file__.startswith(self.ipython_extension_dir):
     82                         print(("Loading extensions from {dir} is deprecated. "

/usr/lib/python3.6/importlib/__init__.py in import_module(name, package)
    124                 break
    125             level += 1
--> 126     return _bootstrap._gcd_import(name[level:], package, level)
    127 
    128 

/usr/lib/python3.6/importlib/_bootstrap.py in _gcd_import(name, package, level)

/usr/lib/python3.6/importlib/_bootstrap.py in _find_and_load(name, import_)

/usr/lib/python3.6/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

ModuleNotFoundError: No module named 'RFigure.RfigureMagics'

In [ ]: