diff --git a/python/prebuild.py b/python/prebuild.py index 4fda696..00db531 100644 --- a/python/prebuild.py +++ b/python/prebuild.py @@ -29,7 +29,7 @@ def gen_pyplot_functions(dub_root): functions = filter(lambda i: i[0] != '_' or i[0] in ascii_lowercase, extract_function_names(matplotlib.pyplot)) - with open(dub_root + "/views/pyplot_functions.txt", "w") as f: + with open(dub_root + "/views/pyplot_functions.txt", "w+") as f: f.write("\n".join(functions)) diff --git a/source/matplotlibd/core/translate.d b/source/matplotlibd/core/translate.d index 1819117..04fdab6 100644 --- a/source/matplotlibd/core/translate.d +++ b/source/matplotlibd/core/translate.d @@ -1,5 +1,7 @@ module matplotlibd.core.translate; +import std.traits : isArray; + alias immutable bool PyBool; alias immutable (void*) PyNone; @@ -19,6 +21,9 @@ string d2py(T)(T v) { else static if (is(typeof(v) : string)) return format("\"%s\"", v); + else static if(isArray!T) + return format("np.array(%s)", v); + else return format("%s", v); } @@ -33,6 +38,8 @@ unittest { assert(d2py(false) == "False"); assert(d2py("Hello!") == "\"Hello!\""); assert(d2py(5.iota) == "[0, 1, 2, 3, 4]"); + import std.array : array; + assert(d2py(5.iota.array) == "np.array([0, 1, 2, 3, 4])"); } diff --git a/source/matplotlibd/pyplot.d b/source/matplotlibd/pyplot.d index 5d7949f..78e8d09 100644 --- a/source/matplotlibd/pyplot.d +++ b/source/matplotlibd/pyplot.d @@ -4,7 +4,7 @@ import matplotlibd.core.translate; private: -string py_script = "import matplotlib.pyplot as plt\n"; +string py_script = "import matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nimport numpy as np\n"; immutable string plt_funcs = (){ import std.string: splitLines; @@ -35,16 +35,16 @@ import matplotlibd.core.translate: False, True, None; void clear() { - py_script = "import matplotlib.pyplot as plt\n"; + py_script = "import matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nimport numpy as np\n"; } mixin(plt_funcs); unittest { import std.string; - auto script = py_script ~ "plt.plot([1, 2],[2, 4],\"r-\",lw=2)\n"; + auto script = py_script ~ "plt.plot(np.array([1, 2]),np.array([2, 4]),\"r-\",lw=2)\n"; plot([1, 2], [2, 4], "r-", ["lw": 2]); assert(script == py_script); clear(); - assert(py_script == "import matplotlib.pyplot as plt\n"); + assert(py_script == "import matplotlib.pyplot as plt\nimport numpy as np\n"); } diff --git a/views/keepme b/views/keepme new file mode 100644 index 0000000..e69de29 diff --git a/views/pyplot_functions.txt b/views/pyplot_functions.txt deleted file mode 100644 index 0aec43d..0000000 --- a/views/pyplot_functions.txt +++ /dev/null @@ -1,225 +0,0 @@ -Annotation -Arrow -Artist -AutoLocator -Axes -Button -Circle -Figure -FigureCanvasBase -FixedFormatter -FixedLocator -FormatStrFormatter -Formatter -FuncFormatter -GridSpec -IndexLocator -Line2D -LinearLocator -Locator -LogFormatter -LogFormatterExponent -LogFormatterMathtext -LogLocator -MaxNLocator -MultipleLocator -Normalize -NullFormatter -NullLocator -PolarAxes -Polygon -Rectangle -ScalarFormatter -Slider -Subplot -SubplotTool -Text -TickHelper -Widget -_auto_draw_if_interactive -_autogen_docstring -_backend_selection -_imread -_imsave -_setp -_setup_pyplot_info_docstrings -_show -_string_to_bool -acorr -angle_spectrum -annotate -arrow -autoscale -autumn -axes -axhline -axhspan -axis -axvline -axvspan -bar -barbs -barh -bone -box -boxplot -broken_barh -cla -clabel -clf -clim -close -cohere -colorbar -colormaps -colors -connect -contour -contourf -cool -copper -csd -cycler -dedent -delaxes -disconnect -draw -draw_all -draw_if_interactive -errorbar -eventplot -figaspect -figimage -figlegend -fignum_exists -figtext -figure -fill -fill_between -fill_betweenx -findobj -flag -gca -gcf -gci -get -get_backend -get_cmap -get_current_fig_manager -get_figlabels -get_fignums -get_plot_commands -get_scale_docs -get_scale_names -getp -ginput -gray -grid -hexbin -hist -hist2d -hlines -hold -hot -hsv -imread -imsave -imshow -inferno -install_repl_displayhook -interactive -ioff -ion -is_numlike -is_string_like -ishold -isinteractive -jet -legend -locator_params -loglog -magma -magnitude_spectrum -margins -matshow -minorticks_off -minorticks_on -new_figure_manager -over -pause -pcolor -pcolormesh -phase_spectrum -pie -pink -plasma -plot -plot_date -plotfile -plotting -polar -prism -psd -pylab_setup -quiver -quiverkey -rc -rc_context -rcdefaults -register_cmap -rgrids -savefig -sca -scatter -sci -semilogx -semilogy -set_cmap -setp -show -silent_list -specgram -spectral -spring -spy -stackplot -stem -step -streamplot -subplot -subplot2grid -subplot_tool -subplots -subplots_adjust -summer -suptitle -switch_backend -table -text -thetagrids -tick_params -ticklabel_format -tight_layout -title -tricontour -tricontourf -tripcolor -triplot -twinx -twiny -uninstall_repl_displayhook -violinplot -viridis -vlines -waitforbuttonpress -winter -xcorr -xkcd -xlabel -xlim -xscale -xticks -ylabel -ylim -yscale -yticks \ No newline at end of file