wxPen, some possible enhancements...

I've been working on a pen-editing control, and in doing so, I've created a few enhancements to the wxPen class.

    * __eq__ and (useful) __repr__ method (note that the __eq__ method
      below assumes the presence of a coerce class-method constructor
      which isn't present in the base wxPen class)
    * constructor w/ options for specifying all data-values (this should
      probably be added at the C++ level, but the Python level is
      capable of doing it if necessary)

from wxPython.wx import *

class wxPen(wxPyPen):
    """A somewhat easier-to-use Pen class for use with basictypes"""
    dataType = "wx.pen"
    coreAttributes = ('colour','width','style','cap','join',)
    extraAttributes = ('stipple','dashes')
    def __init__(
        self,
        colour="BLACK",
        width=1,
        style=wxSOLID,
        cap=wxCAP_ROUND,
        join=wxJOIN_ROUND,
        stipple=None,
        dashes=None,
    ):
        """Initialize the wxPen object

        colour -- wxColour specifier, a wxColour, a named colour
            or a #ffffff formatted string value
        width -- pen-width in pixels
        style -- one of the wxPen style constants
            wxSOLID
            wxVERTICAL_HATCH
            wxUSER_DASH
            wxCROSSDIAG_HATCH
            wxHORIZONTAL_HATCH
            wxSTIPPLE
            wxBDIAGONAL_HATCH
            wxFDIAGONAL_HATCH
            wxDOT_DASH
            wxSHORT_DASH
            wxLONG_DASH
            wxCROSS_HATCH
            wxTRANSPARENT
        cap -- one of the wxPen cap constants
            wxCAP_ROUND
            wxCAP_BUTT
            wxCAP_PROJECTING
        join -- one of the wxPen join constants
            wxJOIN_BEVEL
            wxJOIN_MITER
            wxJOIN_ROUND
        stipple -- when style == wxSTIPPLE, a bitmap used to
            control the drawing style
        dashes -- when style == wxUSER_DASH, an array used to
            control the drawing style
            XXX what is the array of? lengths? I assume it's
                just a python list of something, but I don't
                know what.
        """
        wxPyPen.__init__( self, colour, width, style )
        self.SetJoin( join )
        self.SetCap( cap )
        if style == wxSTIPPLE and stipple is not None:
            self.SetStipple( stipple )
        elif style == wxUSER_DASH and dashes is not None:
            self.SetDashes( dashes )
    def coreValues( self ):
        """Get the core values for this instance"""
        return dict([
            (attr,getattr(self,'Get%s'%attr.capitalize())())
            for attr in self.coreAttributes
        ])
    def __repr__( self ):
        """Get a nice debugging representation of this object"""
        v = self.coreValues()
        v = ", ".join([
            '%s=%r'%(attr,v.get(attr))
            for attr in self.coreAttributes
            if v.get(attr) is not None
        ])
        return "%s(%s)"%( self.__class__.__name__, v)

    def __eq__( self, other ):
        """Compare our core values to pen defined in other"""
        if not isinstance( other, wxPen):
            other = self.__class__.coerce( other )
        a,b = self.coreValues(), other.coreValues()
        if a != b:
            return 0
        # possibility of a stipple or dashes type diff
        if a['style'] == wxSTIPPLE:
            return self.GetStipple() == other.GetStipple()
        elif a['style'] == wxUSER_DASH:
            return self.GetDashes() == other.GetDashes()
        else:
            return 1

Enjoy all,
Mike

···

_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/