Hot answers tagged

18

You should consider using the sandbox functionality. You can create a subkernel and put it in sandbox mode this way: link = LinkLaunch[First[$CommandLine]<> " -wstp -noicon"]; LinkWrite[link, Unevaluated@EvaluatePacket[Developer`StartProtectedMode[]]]; You can then interact with this subkernel using the standard LinkWrite and LinkRead functions. If ...


14

The definitions aren't being lost, they're being shadowed, as described in the tutorial on contexts. Mathematica doesn't warn you about this because it only warns when there is shadowing between contexts that are listed in the $ContextPath. Since Begin only changes $Context and not $ContextPath, you don't get a warning when the symbol that causes shadowing ...


13

I have been solving exactly the same problem about 2 years ago (http://community.wolfram.com/groups/-/m/t/125587?p_p_auth=aZGMz5bs). Students are uploading piece of Mathematica (Wolfram Language) code which is run by a testing script (in Mathematica) and the results are compared with a reference solution. To prevent the students to run potentially dangerous ...


10

I think the symbols sym and $m7res are created by Information. They are not present when the kernel is started. Fresh kernel 1: Quit[] Names["Global`*"] (* {} *) Fresh kernel 2: Quit[] foo = Trace[ Information["Global`*"], TraceInternal -> True]; foo[[8, 3, 5, 7, 2, 9, 18, 65, 2, 1, 3, 6, 7, 5, 6, 4, 3, 3, 6, 2, 8, 12, 10, 5, 6, 2, ...


8

I've certainly encountered this behavior before. While I can't speak authoritatively, I'd think this is as designed, although it does introduce certain inconsistency. I also think that this issue is a result of clash of cultures: the end user - oriented one from the earlier days of Mathematica, and the one coming from standard software-engineering practices. ...


8

Indeed confusing, but can be explained. Please read here first on how Mathematica searches contexts for symbols. In short, $Context tells it where to create symbols. It's for creation, not for lookup. $ContextPath tells it where to look for symbols but doesn't affect symbol creation. If the symbol is not found in any of the $ContextPath contexts, ...


7

And if there is a possibility to manually use it for something useful? Yes, it solves a problem I had many times and which I had to work around. And none of those work arounds was as general as a following method. The case you are writing a package that exports a function GenerateModule[] which creates new notebook/cdf. (let's say each instance has ...


6

If I understand the problem I think I can help, but what I propose is a bit weird. I think you have a package that uses the unqualified Symbol name Order internally, and you need the package not to see the System`Order Symbol while it is defined. To effect this you can temporarily change the Context of Order, then put it back after loading the package. As ...


5

ReadList seems to work as you intended when used instead of Get in your code: ClearAll["file1`*", "file2`*"] Begin["file1`"] ReadList["file1.txt", "Expression"] End[] Begin["file2`"] ReadList["file2.txt", "Expression"] End[] On my system, I obtain: ?file1`* Alternatively you can of course check the values themselves: a (* Out: a *) ...


5

We can replace something, in all definitions associated with a symbol, using function like this: ClearAll[replaceExtendedDefinition] SetAttributes[replaceExtendedDefinition, HoldFirst] replaceExtendedDefinition[sym_, rules_] := Replace[ Language`ExtendedDefinition[sym] , (rule:Rule | RuleDelayed)[lhs_, rhs_] :> ...


4

Note: shown below is an answer to the first version of the OP's question Here is a simple example using scoping constructs, namely Block in this case. The idea is that you can indicate symbols to make local to Block, which implements dynamic scoping: take a look at the "Background and Context" section of its docs for a more complete explanation. Symbols ...


4

As @MarcoB states, you should probably use scoping constructs... If you're opposed to that idea, you can set the Notebooks default context to be Unique to Each Cell Group. I wouldn't recommend that, but it works. You can set that under Evaluation > Notebook's Default Context > Unique to Each Cell Group: Note, to escape this, you need to specify ...


4

There seems to be no way to fool the parsing, so you will either have to store the symbols in a separate file, or use some form of ToExpression. What I would suggest is to use a softer version of ToExpression, and represent the r.h.s. as boxes rather than as a string. Here is what I mean: BeginPackage["myPackage`"]; load; boxed; $context = $Context; ...


3

This is sort of what the `Private` context is meant to be doing for you. You write that you just need more contexts that can behave like Private context for organizational purposes, and this can be achieved using subcontexts that use a `Private` mark. My normal instinct would be to put the helper functions to MainFunction inside a `Private` branch of ...


3

What features I/we/developers need: allowing (only) MyPackage` to use particular non System` packages e.g. using GeneralUtilities` functions after BeginPackage["MyPackage`"] allowing user to use particular non System` packages (only) after loading MyPackage` e.g. After << MyPackage` I would like to be able to call PrintDefinitions from ...


3

It returns 0 because you are doing something like: D[ Sin[Global`x1], MyP`Private`x1] (*Global` or current $Context really*) Why? You can read more in a related topic: Behavior of Remove inside a Package and in the general one I'm encouraging you to become familiar with: How symbol lookup actually works There are some tricks available to detect a ...


3

OP wrote: I want to be able to simply call Needs["Test`"] and have functions such as Test`f available for use inside ParallelTable without any further calling of $DistributedContexts or DistributeDefinitions We had a similar problem designing mathStatica functions to operate in a parallel environment. There can be varying degrees of complexity. For ...


3

Shortly: Here what is going on, aTestFunction was created in aPackage` context, then you've removed it and since the current context is aPackage`Private` the definition of your function is created there. More precisely: New symbols are created in current $Context during read time, (1) the existance of aTestFunction between BeginPackage and Begin lines ...


3

is there some way to take the call ReplaceAll[var, rules]and make it forget contexts for a minute? BeginPackage["Test`"]; replace::usage = "Returns var/.rules."; print; Begin["`Private`"]; replace[rules_] := ReplaceAll[Symbol[$Context <> "var"], rules] End[]; EndPackage[]; Not general, but works. I would expect that replace[rules_] := ...


3

You can make use of Formal Symbols. BeginPackage["Test`"]; replace::usage = "Returns \[FormalV]/.rules."; Begin["`Private`"]; replace[rules_] := \[FormalV] /.rules End[]; EndPackage[]; Then in the notebook. replace[\[FormalV] -> 0] (* 0 *) Hope this helps.


3

I think this is a caching problem. Considering the examples in the Update documentation it certainly makes sense that this could be the case. Although Update appears to have no effect I have found something that does: Information. x = 1; Context[x] = "foo`"; x foo`x Context[x] 1 1 "foo`" ?x foo`x x=1 x foo`x Context[x] x 1 "Global`" ...


3

Every symbol always has a context. Most of the time we are in the Global context. $Context (* "Global`" *) Normally we don't have to specify a variable as Global`s Simply writing s is sufficient provided we are in the Global context. Inside your package the symbols and functions are defined in the SimulatorV2Debug`Private context. In the function ...


3

I see the same behaviour, also using v10.2.0 on Win7/64. As to the "why", the sequence of events is as follows. As a matter of routine, Information generates a random cell tag for the display cell: This cell tag is generated using CreateUUID. CreateUUID, in turn, is autoloaded as part of a collection symbols all somewhat related to cloud functionality. ...


3

This is messy and imperfect, but it will work in simple cases: Import the package contents: pack = ImportString[data, {"Package", "HeldExpressions"}] (* {HoldComplete[BeginPackage["Test`"]], HoldComplete[testFunction::usage = "-";], HoldComplete[Begin["`Private`"]], HoldComplete[testFunction[] := {123, explicitvalue, Hold[explicitvalue]};], ...


2

i am doing the same thing as you did,and i am using MSP module which is a security solution for webMathmatica。please refer my topic How to adapt MSPToExpression function in $PrePrint? sandbox seems another good solution for security,mathematica online is using it。 can i ask whether you solved this by using sandbox?


2

Try replace[rules_] := ReplaceAll[Global`var, rules] in your function. Then replace[var -> 0] replace[Test`Private`var -> 0] yields 0 var as I think you'd like.


1

You could store symbols related to separate equation systems in separate contexts. This way you could add/remove those additional contexts, to/from $ContextPath, at will. How to automate assigning of appropriate symbols to appropriate contexts depends on how exactly you're defining your equation systems. You could for example post-process definition of your ...


1

What's happening is that the expression is being read in and parsed before being evaluated, which means that the symbols Symbolize and ParsedBoxWrapper are created before the "Notation`" context is placed on the $ContextPath by Needs. Without seeing the symbols in "Notation`", Mathematica creates them in the "Global`" context, leading to the shadowing. You ...


1

To summarize the comments by me and Szabolcs into an answer, these will be running in different instances (separate processes) of the same kernel executable. There is a related note in the documentation of the WolframScript interpreter, Each of the Wolfram Language scripts running concurrently starts its own kernel, with no shared variables or ...



Only top voted, non community-wiki answers of a minimum length are eligible