Package peppy :: Package lib :: Module userparams
[frames] | no frames]

Module userparams

Helpers to create user preferences for class attribute defaults and
editing widgets for user modification of preferences.

This module is used to create preferences that can be easily saved to
configuration files.  It is designed to be class-based, not instance
based.

Classes need to inherit from ClassPrefs and then define a class
attribute called default_classprefs that is a tuple of Param objects.
Subclasses will inherit the preferences of their parent classes, and
can either redefine the defaults or add new parameters.  For example:

  class Vehicle(ClassPrefs):
      default_classprefs = (
          IntParam('wheels', 0, 'Number of wheels on the vehicle'),
          IntParam('doors', 0, 'Number of doors on the vehicle'),
          BoolParam('operational', True, 'Is it running?'),
          BoolParam('engine', False, 'Does it have a motor?'),
          )

  class Car(Vehicle):
      default_classprefs = (
          IntParam('wheels', 4),
          IntParam('doors', 4),
          BoolParam('engine', True),
          StrParam('license_state', '', 'State in which registered'),
          StrParam('license_plate', '', 'License plate number'),
          )

  class WxCar(wx.Window, Car):
      default_classprefs = (
          IntParam('pixel_height', 400),
          IntParam('pixel_width', 600),
          )

The metaclass for ClassPrefs processes the default_classprefs and adds
another class attribute called classprefs that is a proxy object into
a global preferences object.

The global preferences object GlobalPrefs uses the ConfigParser module
to serialize and unserialize user preferences.  Since the user
preferences are stored in text files, the Param objects turn the user
text into the expected type of the Param so that your python code only
has to deal with the expected type and doesn't have to do any conversion
itself.

The user configuration for the above example could look like this:

  [Vehicle]
  operational = False
  
  [Car]
  license_state = CA
  doors = 2
  
  [WxCar]
  pixel_width = 300
  pixel_height = 200

and the GlobalPrefs.readConfig method will parse the file,
interpreting the section name as the class.  It will set
Vehicle.classprefs.operational to be the boolean value False,
Car.classprefs.license_state to the string 'CA', Car.classprefs.doors to
the integer value 2, etc.

Your code doesn't need to know anything about the conversion from the
string to the expected type -- it's all handled by the ClassPrefs and
GlobalPrefs.

The PrefDialog class provides the user interface dialog to modify
class parameters.  It creates editing widgets that correspond to the
Param class -- for BoolParam it creates a checkbox, IntParam a text
entry widget, ChoiceParam a pulldown list, etc.  The help text is
displayed as a tooltip over the editing widget.  User subclasses of
Param are possible as well.

Classes
  FileBrowseButton2
Small enhancements to FileBrowseButton
  DirBrowseButton2
Update to dir browse button to browse to the currently set directory instead of always using the initial directory.
  Param
Generic param interface.
  ReadOnlyParam
Read-only parameter for display only.
  ParamSection
  BoolParam
Boolean parameter that displays a checkbox as its user interface.
  IntParam
Int parameter that displays a text entry field as its user interface.
  FloatParam
Int parameter that displays a text entry field as its user interface.
  StrParam
String parameter that displays a text entry field as its user interface.
  UnquotedStrParam
String parameter that doesn't enclose the string in quotes
  DateParam
Date parameter that displays a DatePickerCtrl as its user interface.
  DirParam
Directory parameter that displays a DirBrowseButton2 as its user interface.
  PathParam
Directory parameter that displays a FileBrowseButton as its user interface.
  ChoiceParam
Parameter that is restricted to a string from a list of choices.
  IndexChoiceParam
Parameter that is restricted to a string from a list of choices, but using an integer as the value.
  KeyedIndexChoiceParam
Parameter that is restricted to a string from a list of choices, but using a key corresponding to the string as the value.
  FontParam
Font parameter that pops up a font dialog.
  GlobalPrefs
  PrefsProxy
Dictionary-like object to provide global prefs to a class.
  ClassPrefsMetaClass
  ClassPrefs
Base class to extend in order to support class prefs.
  PrefPanel
Panel that shows ui controls corresponding to all preferences
  PrefClassList
  PrefPage
Notebook page to hold preferences for one notebook tab
  PrefDialog
Functions
 
getAllSubclassesOf(parent=<class 'peppy.debug.debugmixin'>, subclassof=None)
Recursive call to get all classes that have a specified class in their ancestry.
 
parents(c, seen=None)
Python class base-finder.
 
getClassHierarchy(klass, debug=0)
Get class hierarchy of a class using global class cache.
 
getHierarchy(obj, debug=0)
Get class hierarchy of an object using global class cache.
 
getNameHierarchy(obj, debug=0)
Get the class name hierarchy.
 
getSubclassHierarchy(obj, subclass, debug=0)
Return class hierarchy of a particular subclass.
Variables
  parentclasses = {<class 'wx._windows.Panel'>: [], <class 'wx._...
  skipclasses = ['debugmixin', 'ClassPrefs', 'object']
Function Details

getAllSubclassesOf(parent=<class 'peppy.debug.debugmixin'>, subclassof=None)

 

Recursive call to get all classes that have a specified class in their ancestry. The call to __subclasses__ only finds the direct, child subclasses of an object, so to find grandchildren and objects further down the tree, we have to go recursively down each subclasses hierarchy to see if the subclasses are of the type we want.

Parameters:
  • parent (class) - class used to find subclasses
  • subclassof (class) - class used to verify type during recursive calls
Returns:
list of classes

parents(c, seen=None)

 

Python class base-finder.

Find the list of parent classes of the given class. Works with either old style or new style classes. From http://mail.python.org/pipermail/python-list/2002-November/132750.html

getClassHierarchy(klass, debug=0)

 

Get class hierarchy of a class using global class cache.

If the class has already been seen, it will be pulled from the cache and the results will be immediately returned. If not, the hierarchy is generated, stored for future reference, and returned.

Parameters:
  • klass - class of interest
Returns:
list of parent classes

getHierarchy(obj, debug=0)

 

Get class hierarchy of an object using global class cache.

If the class has already been seen, it will be pulled from the cache and the results will be immediately returned. If not, the hierarchy is generated, stored for future reference, and returned.

Parameters:
  • obj - object of interest
Returns:
list of parent classes

getNameHierarchy(obj, debug=0)

 

Get the class name hierarchy.

Given an object, return a list of names of parent classes.

Similar to getHierarchy, except returns the text names of the classes instead of the class objects themselves.

Parameters:
  • obj - object of interest
Returns:
list of strings naming each parent class

getSubclassHierarchy(obj, subclass, debug=0)

 

Return class hierarchy of a particular subclass.

Given an object, return the hierarchy of classes that are subclasses of a given type. In other words, this filters the output of getHierarchy such that only the classes that are descended from the desired subclass are returned.

Parameters:
  • obj - object of interest
  • subclass - subclass type on which to filter
Returns:
list of strings naming each parent class

Variables Details

parentclasses

Value:
{<class 'wx._windows.Panel'>: [],
 <class 'wx._core.Window'>: [],
 <class 'wx._controls.CheckListBox'>: [],
 <class 'wx._controls.ListCtrl'>: [],
 <class 'wx._controls.TreeCtrl'>: [],
 <class 'wx._controls.PyControl'>: [],
 <class 'peppy.debug.debugmixin'>: [],
 <class 'peppy.stcinterface.STCInterface'>: [<class 'peppy.stcinterfac\
...