Skip to main content

formatting - What is the most convenient way to read definitions of in-memory symbols when we don't have the source files? (Spelunking tools)


Note: I put Simon's implementation on GitHub. Contributions welcome!





When trying to read the definition of already defined (package or built-in) symbols using Information or FullDefinition, the biggest inconvenience is that lots of distracting private context names appear in front of all symbol names.


Currently I am using a little function contextFreeDefinition[] to avoid this problem. It will attempt to hide the most frequently appearing context name in the definition. contextFreeDefinition[] is based on this answer.


Compare for example ClearAttributes[RunThrough, ReadProtected]; Information[RunThrough] and contextFreeDefinition[RunThrough]. The latter is a lot less cluttered because the System`Dump` context is hidden in the definition. (I usually paste the output of this function into Workbench and re-indent it using the Source -> Format context menu item for better readability)




Unfortunately contextFreeDefinition[] does not always successfully hide contexts, for example try the following:


ImportString["1", "List"]; (* force Stub symbols to be loaded *)

System`Convert`TableDump`ImportList // contextFreeDefinition


and notice that several symbols (especially patterns) still have System`Convert`TableDump` prepended. For example, I see the following in the FullDefinition it prints:


protectRegEx[System`Convert`TableDump`s_String] := 
StringReplace[System`Convert`TableDump`s, $ProtectedCharacterRules]

The symbol System`Convert`TableDump`s still has the context name prepended even though the function tried to hide exactly this context.


Question: How can contextFreeDefinition[] be fixed so it always hides the context, or what other alternative approaches are there to read the definitions of in-memory symbols?




The code of contextFreeDefinition[].


Clear[commonestContexts, contextFreeDefinition]


commonestContexts[sym_Symbol, n_: 1] := Quiet[
Commonest[
Cases[Level[DownValues[sym], {-1}, HoldComplete],
s_Symbol /; FreeQ[$ContextPath, Context[s]] :> Context[s]], n],
Commonest::dstlms]

contextFreeDefinition::contexts = "Not showing the following contexts: `1`";

contextFreeDefinition[sym_Symbol, contexts_List] :=
(If[contexts =!= {}, Message[contextFreeDefinition::contexts, contexts]];

Internal`InheritedBlock[{sym}, ClearAttributes[sym, ReadProtected];
Block[{$ContextPath = Join[$ContextPath, contexts]},
Print@InputForm[FullDefinition[sym]]]])

contextFreeDefinition[sym_Symbol, context_String] :=
contextFreeDefinition[sym, {context}]

contextFreeDefinition[sym_Symbol] :=
contextFreeDefinition[sym, commonestContexts[sym]]




Understanding and using the function:


commonestContexts[sym, n] will find the n most frequently used contexts that are not in $ContextPath in the definition of symbol sym.


contextFreeDefinition[sym] will print the FullDefinition of sym, hiding the commonest context that would appear there. It will also issue a message with the name of the context being hidden.


contextFreeDefinition[sym, {"Context1`", "Context2`", ...}] will try to hide an explicitly given list of contexts.



Answer



Link to the code on GitHub




I have been using this. It's mostly Leonid's code from the stackoverflow question you linked to, but it uses Definition instead of DownValues. Symbol names are printed without any context, but the full symbol name is put into a Tooltip so you can always find out what context a symbol is in.


Update



FullDefinition[symbol] claims to "print the definitions given for symbol, and all symbols on which these depend", but sometimes one wants to explore deeper than the first level of dependency. Here is a version of Spelunk which uses plain Definition instead of FullDefinition, but allows you to click on symbols in the definition to get their definition. So you can dig right down into the dependency chain.


Update 2


The code now copes with definitions containing strings with backticks in, and cases where Definition throws an error.


Also, it now works for symbols which have OwnValues, e.g. Internal`$VideoEncodings.


BeginPackage["Spelunk`"];

Spelunk::usage = "Spelunk[symbol]";

Begin["`Private`"];


defboxes[symbol_Symbol] := Hold[symbol] /. _[sym_] :>
If[MemberQ[Attributes[sym], Locked], "Locked",
Internal`InheritedBlock[{sym},
Unprotect[sym]; ClearAttributes[sym, ReadProtected];
Quiet@Check[ToBoxes[Definition@sym], "DefError"] /.
InterpretationBox[a_, b___] :> a ]];

defboxes[s_String] := defboxes[#] &@ToExpression[s, InputForm, Unevaluated]

prettyboxes[boxes_] :=

boxes /. {" "} -> {"\n-----------\n"} //. {RowBox[{left___, ";",
next : Except["\n"], right___}] :>
RowBox[{left, ";", "\n", "\t", next, right}],
RowBox[{sc : ("Block" | "Module" | "With"), "[",
RowBox[{vars_, ",", body_}], "]"}] :>
RowBox[{sc, "[", RowBox[{vars, ",", "\n\t", body}], "]"}]};

fancydefinition[symbol_Symbol] :=
Cell[BoxData[
prettyboxes[

defboxes[symbol] /.
s_String?(StringMatchQ[#, __ ~~ "`" ~~ __] &) :>
First@StringCases[s,
a : (__ ~~ "`" ~~ b__) :> processsymbol[a, b]]]], "Output",
Background -> RGBColor[1, 0.95, 0.9],
CellGroupingRules->"OutputGrouping",
GeneratedCell->True,
CellAutoOverwrite->True,
ShowAutoStyles->True,
LanguageCategory->"Mathematica",

FontWeight->"Bold"
];

processsymbol[a_, b_] := Module[{db},
Which[
! StringFreeQ[a, "\""], a,
! StringFreeQ[a, "_"] || (db = defboxes[a]) === "Null",
TooltipBox[b, a],
db === "Locked", TooltipBox[b, a <> "\nLocked Symbol"],
db === "DefError", TooltipBox[b, a <> "\nError getting Definition"],

True, ButtonBox[TooltipBox[b, a], ButtonFunction :> Spelunk@a,
BaseStyle -> {}, Evaluator -> Automatic]]]

Spelunk[symbol_Symbol] := CellPrint[fancydefinition[symbol]];

Spelunk[s_String] := CellPrint[fancydefinition[#] &@ToExpression[s, InputForm, Unevaluated]];

SetAttributes[{defboxes, fancydefinition, Spelunk}, HoldFirst]

End[];


EndPackage[];

Comments

Popular posts from this blog

front end - keyboard shortcut to invoke Insert new matrix

I frequently need to type in some matrices, and the menu command Insert > Table/Matrix > New... allows matrices with lines drawn between columns and rows, which is very helpful. I would like to make a keyboard shortcut for it, but cannot find the relevant frontend token command (4209405) for it. Since the FullForm[] and InputForm[] of matrices with lines drawn between rows and columns is the same as those without lines, it's hard to do this via 3rd party system-wide text expanders (e.g. autohotkey or atext on mac). How does one assign a keyboard shortcut for the menu item Insert > Table/Matrix > New... , preferably using only mathematica? Thanks! Answer In the MenuSetup.tr (for linux located in the $InstallationDirectory/SystemFiles/FrontEnd/TextResources/X/ directory), I changed the line MenuItem["&New...", "CreateGridBoxDialog"] to read MenuItem["&New...", "CreateGridBoxDialog", MenuKey["m", Modifiers-...

How to thread a list

I have data in format data = {{a1, a2}, {b1, b2}, {c1, c2}, {d1, d2}} Tableform: I want to thread it to : tdata = {{{a1, b1}, {a2, b2}}, {{a1, c1}, {a2, c2}}, {{a1, d1}, {a2, d2}}} Tableform: And I would like to do better then pseudofunction[n_] := Transpose[{data2[[1]], data2[[n]]}]; SetAttributes[pseudofunction, Listable]; Range[2, 4] // pseudofunction Here is my benchmark data, where data3 is normal sample of real data. data3 = Drop[ExcelWorkBook[[Column1 ;; Column4]], None, 1]; data2 = {a #, b #, c #, d #} & /@ Range[1, 10^5]; data = RandomReal[{0, 1}, {10^6, 4}]; Here is my benchmark code kptnw[list_] := Transpose[{Table[First@#, {Length@# - 1}], Rest@#}, {3, 1, 2}] &@list kptnw2[list_] := Transpose[{ConstantArray[First@#, Length@# - 1], Rest@#}, {3, 1, 2}] &@list OleksandrR[list_] := Flatten[Outer[List, List@First[list], Rest[list], 1], {{2}, {1, 4}}] paradox2[list_] := Partition[Riffle[list[[1]], #], 2] & /@ Drop[list, 1] RM[list_] := FoldList[Transpose[{First@li...

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...