Skip to content

Commit ddbcb0c

Browse files
committed
Added options in a more flexible way.
Fixed readme
1 parent 3af47e1 commit ddbcb0c

File tree

5 files changed

+1947
-84
lines changed

5 files changed

+1947
-84
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
.project
12
.DS_Store

README.md

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,6 @@
22

33
Mathematica package to convert [MATHEMATICA](https://www.wolfram.com/mathematica/) expressions to Python [Numpy](http://www.numpy.org/)
44

5-
## Input arguments
6-
7-
`x`: your mathematica expression, it can be `numbers`, `literals`,
8-
`complexes` or `lists`;
9-
`numpy\[LetterSpace]prefix`: string defining your Numpy `import` prefix,
10-
e.g.:
11-
* if your used `import numpy as np`, your prefix should be the string
12-
`np`
13-
* if your used `from numpy import *`, your prefix should be the empty
14-
string `''`
15-
16-
## Output
17-
18-
* The Numpy python-ready expression (to be copied as a string)
19-
* Optionally, the formatted expression can be copied to your clipboard automatically
20-
215
## Installation
226

237
To install the package
@@ -28,6 +12,28 @@ To install the package
2812

2913
You should be ready to go.
3014

15+
16+
## Usage
17+
18+
The package mainly provides the `ToPython` function, which takes a Mathematica expression
19+
and tries to convert it to a python expression. It can handle a lot of expressions
20+
already, but it is obviously limited.
21+
22+
Beside the actual expression the `ToPython` function also supports two options:
23+
24+
* `NumpyPrefix`, which determines the name under which numpy is imported. The default is
25+
to prefix all numpy call with `np.`, but you can also set `NumpyPrefix` to `"numpy"` to
26+
enforce `numpy.` as a prefix. If you supply an empty string, no prefix is added, which
27+
might be useful if you use the wildcard import `fromm numpy import *`
28+
* `Copy`, which when enabled copies the formatted expression to the clipboard
29+
30+
Taken together, a simple example call is
31+
```
32+
ToPython[Sin[x], NumpyPrefix->"numpy", Copy->True]
33+
```
34+
which should copy `numpy.sin(x)` to your clipboard.
35+
36+
3137
## Disclaimer
3238

3339
This has not been tested for every possible combinations of all the things, use at your own risks.

ToPython.wl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ modifications by David Zwicker
1919

2020
BeginPackage["ToPython`"]
2121

22-
ToPython::usage = "ToPython[expression, numpyprefix, copy]
22+
ToPython::usage = "ToPython[expression, NumpyPrefix->\"np\", Copy->False]
2323
converts Mathematica expression to a Numpy compatible expression. Because Numpy can
24-
be imported in several ways, numpyprefix is a string that will be added to appended
25-
to function names, e.g., Cos->np.cos. If copy==True, the result is copied to the clipboard"
24+
be imported in several ways, you can specify the name of the numpy module using the
25+
NumpyPrefix option. The additional option Copy allows you to copy the result to the clipboard"
2626

27-
ToPythonEquation::usage = "ToPythonEquation[equation, numpyprefix, copy] converts a
28-
Mathematica equation to a Numpy compatible express"
27+
ToPythonEquation::usage = "ToPythonEquation[equation, NumpyPrefix->\"np\", Copy->False]
28+
converts a Mathematica equation to a Numpy compatible expression."
2929

3030

3131

@@ -36,8 +36,10 @@ Begin["Private`"]
3636
singleFunctions={Log, Sin, Cos, Tan, Sinh, Cosh, Tanh};
3737

3838

39-
ToPython[expression_, numpyprefix_:"np", copy_:False] := Module[
40-
{result, greekrule, format, PythonForm, np, br, brackets, a, b, l, m, args},
39+
Options[ToPython] = {NumpyPrefix->"np", Copy->False};
40+
ToPython[expression_, OptionsPattern[]] := Module[
41+
{numpyprefix=OptionValue[NumpyPrefix], copy=OptionValue[Copy],
42+
result, greekrule, format, PythonForm, np, br, brackets, a, b, l, m, args},
4143

4244
(* determine the correct numpy prefix *)
4345
If[numpyprefix=="", np=numpyprefix, np=numpyprefix<>"."];
@@ -128,7 +130,8 @@ result
128130
]
129131

130132

131-
ToPythonEquation[Equal[a_, b_], numpyprefix_:"np", copy_:True] := ToPython[a - b, numpyprefix, copy]
133+
Options[ToPythonEquation] = {NumpyPrefix->"np", Copy->False};
134+
ToPythonEquation[Equal[a_, b_], opts : OptionsPattern[]] := ToPython[a - b, opts]
132135

133136

134137
End[]

0 commit comments

Comments
 (0)