Preferences, Graphs and Utilities¶
Preferences¶
A Prefs holds an application’s preferences: it’s an
AttributedObject (see Attributes), with no
supertypes, saved to a file of its own. An application typically reads it
when it starts, and saves it when it quits:
try:
prefs = MyPrefs.read(path) # (a subclass of Prefs)
except Exception:
prefs = MyPrefs()
...
width = prefs.get("gui/width", storeDefault=600, storeType=int,
note="The width of a new window.")
Declaring a preference where it’s first used – with storeDefault, its
type and a note – adds it, with its default, to the file, where the user can
see and change it. applyEditAttrsBox() makes a
preferences dialog of it with no further work (see Tk Widgets).
prefs.addCallback(key, fn) registers a function to call whenever a
preference changes. Since Prefs is Dirtyable, an
application can save it only if it’s changed.
Graphs¶
util.abstractGraph is the basis for persistent graphs: an
AbstractGraph is a database (see
Persistence) of AGObjects, each of
which keeps the relations that come in to it and go out from it. A relation,
AGRelation, is itself an object, with a
src and a dst – either of which may be a node, another relation, or
None – and keeps its ends’ lists up to date as it’s connected, reconnected
and disconnected. (Semantic Nets’ models and views are both
AbstractGraphs.)
The utilities module¶
util.utils is a miscellany. Among other things:
- URLs and files
fixURL()(make a file path or URL into a URL, with an extension),isValidURL(),urlExists(),fileToUrl(),urlToFile(),changeUrlExtension().- Classes
getFullClassName()andgetClassFromFullyQualifiedName()(how saved files name classes),findConcreteSubclasses(),importSubmodules().- Geometry
Point,pointInRect(),pointInPoly(),overlaps(),expandRect(),normalizeRect(),midpoint().- Tk helpers
bindRightMouse()(a right click, or Control-click on a Mac),askUser()(asks in a Tk dialog if there’s a GUI, or else on the terminal),showExceptionDialog(),VerticalScrolledFrame.- Lists and text
grouped(),flattenPairs(),removeDuplicates(),strToBool(),quoteIfNecessary(),resolveAliases()(${name}substitutions).- Commands
makeParserFromFunc(): anargparseparser from a function’s signature and docstring – what the command parser is built on (see Commands).CategoriesNamed categories of objects, each defined by a test (Semantic Nets uses them to hide or erase categories of objects in a view).
ExtendedFlagA Flag whose members can be looked up by combined names, eg:
Flags["A|B"].