Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting current context from within Package
    primarykey
    data
    text
    <p>I had something like the following in my notebook.</p> <pre><code>test1[g_] := (g == 5); test2[g_] := (g == 6); tests={"test1", "test2"} ToExpression[#][5] &amp; /@ tests </code></pre> <p>When I put this code in a package it doesn't work because <code>test1</code> is now called <code>MyPackage'Private'test1</code>. How can I modify the last line to make this code run both inside package and inside notebook?</p> <p><b>Update</b> Here's why I was doing doing ToExpression as opposed to using Symbols. In retrospect, maybe it's easier to use Symbols instead</p> <p>I had a function which I call like <code>getGraphs["LeafFree","Planar",!"Tree",...]</code> to get all graphs that are leaf free, planar and not trees. Some of those strings are classes in <code>GraphData</code>, while others were my own classes. For each of my own classes I had a function with identical name, like <code>LeafFree</code> that tested the property. In the notebook, using <code>ToExpression</code> code like above was the quickest way to implement this.</p> <pre><code>getGraphs[n_Integer, cl__] := getGraphs[{n, n}, cl]; getGraphs[{nmin_Integer, nmax_Integer}, cl__] := Module[{maxgraphnum = 100}, customClasses = {"isLeafFree", ! "isLeafFree"}; classes = {cl}\[Backslash]customClasses; builtinClasses = GraphData["Classes"] \[Tilde] (Not /@ GraphData["Classes"]); Assert[classes \[Subset] builtinClasses]; isLeafFree[gname_] := FreeQ[GraphData[gname, "DegreeSequence"], 0 | 1]; posClasses = Cases[classes\[Backslash]customClasses, _String]; posGroup = If[posClasses == {}, GraphData[nmin ;; nmax], GraphData[posClasses, nmin ;; nmax]]; negClasses = classes\[Backslash]posClasses; negGroups = GraphData[#[[1]], nmin ;; nmax] &amp; /@ negClasses; result = Complement[posGroup, Sequence @@ negGroups]; customTest[g_] := And @@ (ToExpression[#][g] &amp; /@ ({cl} \[Intersection] customClasses)); (*result=Take[result,Min[Length[result],100]];*) result = Select[result, customTest] ] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload