Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6026649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:28:34+00:00 2026-05-23T04:28:34+00:00

I would like to wrap a non-Traits model class for use with Python Traits.

  • 0

I would like to wrap a non-Traits model class for use with Python Traits. My goal is to write a Traits-based UI to manipulate an “external” model class. The external model class has been generated by SWIG and so I cannot add enthought.traits.api.HasTraits as an ancestor (I think, though I may be wrong).

My current best attempt is

from enthought.traits.api import HasStrictTraits, Property, Instance

class ExternalModel():
    foo = 'foo'

class TraitsModel(HasStrictTraits):
    _e = Instance(ExternalModel)

    def __init__(self):
        self._e = ExternalModel()
        self.add_trait('foo', Property(lambda     :getattr(self._e,'foo'     ),
                                       lambda attr:setattr(self._e,'foo',attr)))

which causes the Traits-based class TraitsModel to have a mutable property which delegates to the contained non-Traits ExternalModel instance. However, TraitsModel.trait_names() doesn’t report ‘foo’ as a recognized trait.

Any suggestions for how to have TraitsModel report a ‘foo’ trait which is linked to ExternalModel? enthought.traits.api.DelegatesTo seems to require the target be a Traits class (though I may not have found the right invocation and that is possible).

A more MVC-ish approach is probably to have a Traits-based view of my ExternalModel. I’ve been unable to figure out having a non-Traits model for a Traits-based view. Suggestions in that direction also greatly welcome.

Update I have figured out how to get HasTraits as the ExternalModel superclass using the approach at http://agentzlerich.blogspot.com/2011_05_01_archive.html and it seems to have been a complete waste of time. Apparently the SWIG voodoo and the Traits hoodoo do not jive. Wrapping ExternalModel within TraitsModel as this question asks seems the best route.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:28:35+00:00Added an answer on May 23, 2026 at 4:28 am
    from enthought.traits.api import HasStrictTraits, Instance, Property
    
    class ExternalModel(object):
        foo = 'foo'
    
    class TraitsModel(HasStrictTraits):
        _e = Instance(ExternalModel, ExternalModel())
    
        def __init__(self):
            '''
            >>> wrapper = TraitsModel()
            >>> wrapper.foo
            'foo'
            >>> wrapper._e.foo = 'bar'
            >>> wrapper.foo
            'bar'
            >>> wrapper.trait_names()
            ['trait_added', '_e', 'foo', 'trait_modified']
            '''
            HasStrictTraits.__init__(self)
            for trait in (name for name in dir(self._e) if not name.startswith('__')):
                self.__class__.add_class_trait(
                    trait,
                    Property(
                        lambda:getattr(self._e, trait),
                        lambda attr:setattr(self._e, trait, attr)
                    )
                )
    
    
    if __name__ == '__main__':
        import doctest
        doctest.testmod()
    

    A fairly robust solution is to use the add_class_trait of the HasTraits class, coupled with dir(self._e) to get the names of the class attributes of ExternalModel and a generator expression/list comprehension to filter the magic class method names (filter with an appropriate function would work better for wrapping a more complex class).

    Also:

    • ExternalModel should inherit from object

    • __init__ should call HasStrictTraits.__init__ (or super(HasStrictTraits, self).__init__())

    • _e can also be created in the Instance trait declaration as the second argument using ExternalModel() or even (), or as a method of TraitsModel like:

      def __e_default(self): # note preceding underscore
          return ExternalModel()
      

    Lastly, I have an slightly old copy of the Enthought APIs including Traits which can be very handy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use a language that I am familiar with - Java,
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to test a string containing a path to a file for
I would like to sort an array in ascending order using C/C++ . The
I would like to have a reference for the pros and cons of using
I would like to filter an array of items by using the map() function.
I would like to have an iframe take as much vertical space as it
I would like to have a VM to look at how applications appear and
I would like to quickly send email from the command line. I realize there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.