Access to low and high ETS range attribute settings?

Here is an interactive Python session that uses the Enthought Tool Suite (ETS) components:

>>> import sys
>>> sys.version
'2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)]'
>>> import traits
>>> traits.__version__
'4.5.0'
>>> from traits.api import HasTraits, Range
>>> class Foo(HasTraits):
...     bar = Range (low=1, high=10)
...     
>>> foo = Foo()
>>> foo.bar
1
>>> foo.bar._low
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
AttributeError: 'int' object has no attribute '_low'

I would like to have access to the predefined barinstance attribute limits Foo. How can I do that?

Thanks!

+4
source share
2 answers

The standard way to do this - use features lowand highand assign them as the limitsRange

from traits.api import HasTraits, Range, Int


class Foo(HasTraits):
    high = Int(10)
    low = Int(1)
    bar = Range(high='high', low='low')

Dynamic characteristics can be assigned dynamically:

>>> f = Foo()
>>> f.bar = 5
>>> f.bar
5
>>> f.bar = 30
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/tim/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/traits/trait_types.py", line 1785, in _set
self.error( object, name, value )
  File "/Users/tim/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/traits/trait_handlers.py", line 172, in error value )
traits.trait_errors.TraitError: The 'bar' trait of a Foo instance must be 1 <= a number <= 10, but a value of 30 <type 'int'> was specified.
>>> f.high = 35
>>> f.bar = 30
>>> f.bar
30
+2
source

Range() _low _high, . , , low high Range().

>>> import traits.api
>>> bar = traits.api.Range(low=1, high=10)
>>> bar._low
1
>>> bar._high
10

- , ( bar), foo.traits(), :

>>> foo = Foo()
>>> foo.traits()['bar'] # dictionary of all traits
<traits.traits.CTrait object at 0x000000000525A6D8>
>>> foo.traits()['bar'].trait_type
<traits.trait_types.Range object at 0x0000000005BA3EF0>
>>> foo.traits()['bar'].trait_type._low
1
>>> foo.traits()['bar'].trait_type._high
10
+2

Source: https://habr.com/ru/post/1615632/