1
$\begingroup$

In How to run a Mathematica package (.m) from Python using the “Wolfram Client Library for Python”?, I was able to get the

basicPackage`

example to work, but ran into issues with one of my own packages which depends on Notation`.

See the following MWE, BasicPackageNotation.m, which builds off of a Google groups post

BeginPackage["BasicPackageNotation`","Notation`"];
Unprotect@@Names["BasicPackageNotation`*"];
Unprotect[Evaluate[Context[]<>"*"]];
ClearAll@@Names["BasicPackageNotation`*"];
ClearAll["BasicPackageNotation`Private`*"];

(*ideally at least Symbolize[...] would be uncommented*)
(*Symbolize[ParsedBoxWrapper[SubscriptBox["_","_"]]];*)(*this is the internal representation of Symbolize suggested by Jason Harris for Mma package, see https://groups.google.com/g/comp.soft-sys.math.mathematica/c/rhIhi-v_D5E?pli=1*)

AddTwo::usage = "AddTwo[a, b] returns a+b";

Begin["`Private`"];

AddTwo[a_, b_] := a + b

(*ideally the following version should work - treating Subscript[a, 1] as a Symbol*)
(*AddTwo[[Subscript[a, 1]:_, Subscript[b, 1]:_] := [Subscript[a, 1]+Subscript[b, 1]*)

End[];
Protect@@Names["BasicPackageNotation`*"];
EndPackage[];

When I run this in the Wolfram Client Library for Python:

from wolframclient.evaluation import WolframLanguageSession
from wolframclient.language import wl, wlexpr
with WolframLanguageSession() as s:
    s.evaluate(wl.Get('BasicPackageNotation`'))

I get the following message:

A front end is not available; certain operations require a front end.
It appears that the file 'UnicodeCharacters.tr' has not been included in your installation of Mathematica. The file 'UnicodeCharacters.tr' is necessary for Notation to determine the precedences of characters. Unfortunately Notation will not run without this file. This sometimes occurs in installations inside WRI.
String expected at position 1 in StringToStream[$Failed].
$Failed is not a string, SocketObject, InputStream[ ], or OutputStream[ ].
A front end is not available; certain operations require a front end.
Nonatomic expression expected at position 1 in Select[$Failed, Function[notebookNameMatchQ[Slot[1], Notation Palette]]].
InputAliases is not a known option for Null.
Part 1 of {} does not exist.
Heads Part and List at positions 1 and 2 are expected to be the same.
InputAliases is not a known option for Null.
A front end is not available; certain operations require a front end.
Further output of MessageName[FrontEndObject, notavail] will be suppressed during this calculation.
A front end is not available; certain operations require a front end.
A front end is not available; certain operations require a front end.
A front end is not available; certain operations require a front end.
A front end is not available; certain operations require a front end.
It appears that the file 'UnicodeCharacters.tr' has not been included in your installation of Mathematica. The file 'UnicodeCharacters.tr' is necessary for Notation to determine the precedences of characters. Unfortunately Notation will not run without this file. This sometimes occurs in installations inside WRI.
String expected at position 1 in StringToStream[$Failed].
$Failed is not a string, SocketObject, InputStream[ ], or OutputStream[ ].
A front end is not available; certain operations require a front end.
Nonatomic expression expected at position 1 in Select[$Failed, Function[notebookNameMatchQ[Slot[1], Notation Palette]]].
InputAliases is not a known option for Null.
Part 1 of {} does not exist.
Heads Part and List at positions 1 and 2 are expected to be the same.
InputAliases is not a known option for Null.
A front end is not available; certain operations require a front end.
Further output of MessageName[FrontEndObject, notavail] will be suppressed during this calculation.
A front end is not available; certain operations require a front end.
A front end is not available; certain operations require a front end.
A front end is not available; certain operations require a front end.

The offending line in the package seems to be right at the get-go, with:

BeginPackage["BasicPackageNotation`","Notation`"]; (*no message if you drop the Notation` dependency*)

If I uncomment the Symbolize[] command, then the evaluation seems to just hang (waited maybe ~15 minutes, python kernel still busy, presumably in the wl.Get(...) line)

I realize that many questions involving the Notation package (or other methods for using subscripts as variables/symbols) receive the suggestion "don't do it". Personally, I fall into the camp of using the Notation` package pretty extensively. I've been using Mathematica for HW and academic research for years, and find a lot of benefits to being able to write equations as they'd appear in books or research papers while retaining meaning/usability of the code. I also like the way it can be a fairly clean-looking self-commenting system when dealing with variables with similar meaning (e.g. $a_{in}$, $a_{out}$), and pairs nicely with greek symbols (e.g. $\alpha_{in}$, much nicer than say $\alpha In$ or $\alpha in$). That aside, there are obviously some compatibility issues and nuances such as this one.

Some related questions:

  • Is there little/no hope of using the Notation` package without a FrontEnd?
  • I take it the Wolfram Client Library for Python isn't intended to be used with a FrontEnd? (sounds like kind of a pain even if it's possible)
  • Lets say I have a package that uses many subscripted symbols. Any suggestions for a programmatic way to replace these with compatible (non-subscripted) symbols?
$\endgroup$
1
  • $\begingroup$ The Notation` stuff works by manipulating what MakeExpression/MakeBoxes do. Since those two functions work with the box representation of expressions, they only make sense in the front-end (which can support amd typeset boxes), and not in package files (which only support plain text). From your last point it seems that you already have a package that contains subscripted variables. Did you verify that it works as expected? I would expect it to break for the rrasons above $\endgroup$
    – Lukas Lang
    Commented Jun 28, 2021 at 6:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.