Enthought traits.api traitlist()似乎支持item_validator

发布于 2025-01-17 08:27:55 字数 2341 浏览 2 评论 0原文

在某些应用中,我发现 inthought traits.api 支持Python中的静态变量类型。

我正在尝试使用 item_validator关键字,但是使用item_validator关键字引发了一个错误...我尝试了此问题...

from traits.api import HasTraits, HasRequiredTraits, TraitList, TraitError
from traits.api import Regex, Enum

def garage_item_validator(item):
    """Validate item adjectives and reject pink or floral items"""
    try:
        if isinstance(item, Tool):
            if item.adjective!="pink" or item.adjective!="floral":
                return item
            else:
                raise ValueError()
    except ValueError():
        raise TraitError(f"Cannot put {item} in the Garage()")

class Tool(HasRequiredTraits):
    name = Regex(regex=r"[Ww]rench|[Ll]awnmower", required=True)
    adjective = Enum(*["brown", "rusty", "pink", "floral"], required=True)

    def __init__(self, name, adjective):
        self.name = name
        self.adjective = adjective

    def __repr__(self):
        return """<Tool: {}>""".format(self.name)

class Garage(HasTraits):
    things = TraitList(Tool, item_validator=garage_item_validator)  # <---- TraitList() doesn't work

    def __init__(self):
        self.things = list()

if __name__=="__main__":
    my_garage = Garage()
    my_garage.things.append(Tool("Lawnmower", "brown"))
    my_garage.things.append(Tool("wrench", "pink"))
    print(my_garage)

type> type> typeerror:__init_________init__ ()获得了一个意外的关键字参数'item_validator',尽管特征清单文档明确表示item_validator受支持。

我还尝试使用 traits.api code> code> list list() /a>,但它只是默默地忽略item_validator关键字。

我应该用什么来验证特征列表的内容?

In some applications, I've found that Enthought Traits.api is a helpful addition to support static variable types in python.

I'm trying to use the TraitList() item_validator keyword, but using the item_validator keyword threw an error... I tried this...

from traits.api import HasTraits, HasRequiredTraits, TraitList, TraitError
from traits.api import Regex, Enum

def garage_item_validator(item):
    """Validate item adjectives and reject pink or floral items"""
    try:
        if isinstance(item, Tool):
            if item.adjective!="pink" or item.adjective!="floral":
                return item
            else:
                raise ValueError()
    except ValueError():
        raise TraitError(f"Cannot put {item} in the Garage()")

class Tool(HasRequiredTraits):
    name = Regex(regex=r"[Ww]rench|[Ll]awnmower", required=True)
    adjective = Enum(*["brown", "rusty", "pink", "floral"], required=True)

    def __init__(self, name, adjective):
        self.name = name
        self.adjective = adjective

    def __repr__(self):
        return """<Tool: {}>""".format(self.name)

class Garage(HasTraits):
    things = TraitList(Tool, item_validator=garage_item_validator)  # <---- TraitList() doesn't work

    def __init__(self):
        self.things = list()

if __name__=="__main__":
    my_garage = Garage()
    my_garage.things.append(Tool("Lawnmower", "brown"))
    my_garage.things.append(Tool("wrench", "pink"))
    print(my_garage)

This throws: TypeError: __init__() got an unexpected keyword argument 'item_validator' although the TraitList docs clearly say item_validator is supported.

I also tried to use a traits.api List(), but it just silently ignores the item_validator keyword.

What should I use to validate the contents of a traits list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你与昨日 2025-01-24 08:27:55

TraitList 不是 Trait,而是执行验证和触发事件的 list 的子类。它由 List 特征在内部使用:

>>> from traits.api import HasStrictTraits, List, Int
>>> class Example(HasStrictTraits):
...     x = List(Int) 
... 
>>> example = Example()
>>> example.x
[]
>>> type(example.x)
<class 'traits.trait_list_object.TraitListObject'>
>>> type(example.x).mro()
[<class 'traits.trait_list_object.TraitListObject'>, <class 'traits.trait_list_object.TraitList'>, <class 'list'>, <class 'object'>]

并且它获取由 List 特征设置的 item_validator

>>> example.x.item_validator
<bound method TraitListObject._item_validator of []>

因此,尽管没有钩子可以更改此设置验证器,它确实使用来自 Trait 的验证器,用作 Listtrait 参数(在上面的示例中为 Int,因此列表只会保存整数项)。

>>> example.x.append("five")
Traceback (most recent call last):
...
traits.trait_errors.TraitError: Each element of the 'x' trait of an Example instance must be an integer, but a value of 'five' <class 'str'> was specified.

因此,您可以通过编写验证您想要的方式的自定义特征来实现您的目标。

在您的示例中,您希望这些项目是具有形容词某些属性的 Tool 实例,因此 Instance 特征是一个很好的起点:

from traits.api import BaseInstance, HasTraits, List

class GarageTool(BaseInstance):
    
    def __init__(self, **metadata):
        super().__init__(klass=Tool, allow_none=False, **metadata)
        
    def validate(self, object, name, value):
        # validate it is an instance of Tool
        value = super().validate(object, name, value)
        if value.adjective in {"pink", "floral"}:
            self.error(object, name, value)
        return value
    
    def info(self):
        # give better error messages
        return "a Tool which is neither pink nor floral"

class Garage(HasTraits):
    things = List(GarageTool())
    
if __name__ == "__main__":
    my_garage = Garage()
    my_garage.things.append(Tool("Lawnmower", "brown"))

    # now, pink correctly fails in garage_item_validator()
    my_garage.things.append(Tool("wrench", "pink")) # <-- pink fails

它会根据需要给出错误:

TraitError: Each element of the 'things' trait of a Garage instance must be a Tool which is neither pink nor floral, but a value of <__main__.Tool object at 0x7f899ad89830> <class '__main__.Tool'> was specified.

TraitList isn't a Trait, but rather a subclass of list that performs validation and fires events. It is used internally by the List trait:

>>> from traits.api import HasStrictTraits, List, Int
>>> class Example(HasStrictTraits):
...     x = List(Int) 
... 
>>> example = Example()
>>> example.x
[]
>>> type(example.x)
<class 'traits.trait_list_object.TraitListObject'>
>>> type(example.x).mro()
[<class 'traits.trait_list_object.TraitListObject'>, <class 'traits.trait_list_object.TraitList'>, <class 'list'>, <class 'object'>]

and it gets its item_validator set by the List trait:

>>> example.x.item_validator
<bound method TraitListObject._item_validator of []>

So although there is no hook to change this validator, it does use the validator from the Trait used as the trait argument the List (Int in the above example, so the list will only hold integer items).

>>> example.x.append("five")
Traceback (most recent call last):
...
traits.trait_errors.TraitError: Each element of the 'x' trait of an Example instance must be an integer, but a value of 'five' <class 'str'> was specified.

So you can achieve your goal by writing a custom trait that validates the way that you want.

In your example you want the items to be instances of Tool with certain properties for the adjective, so an Instance trait is a good starting point:

from traits.api import BaseInstance, HasTraits, List

class GarageTool(BaseInstance):
    
    def __init__(self, **metadata):
        super().__init__(klass=Tool, allow_none=False, **metadata)
        
    def validate(self, object, name, value):
        # validate it is an instance of Tool
        value = super().validate(object, name, value)
        if value.adjective in {"pink", "floral"}:
            self.error(object, name, value)
        return value
    
    def info(self):
        # give better error messages
        return "a Tool which is neither pink nor floral"

class Garage(HasTraits):
    things = List(GarageTool())
    
if __name__ == "__main__":
    my_garage = Garage()
    my_garage.things.append(Tool("Lawnmower", "brown"))

    # now, pink correctly fails in garage_item_validator()
    my_garage.things.append(Tool("wrench", "pink")) # <-- pink fails

which gives an error as desired:

TraitError: Each element of the 'things' trait of a Garage instance must be a Tool which is neither pink nor floral, but a value of <__main__.Tool object at 0x7f899ad89830> <class '__main__.Tool'> was specified.
生生漫 2025-01-24 08:27:55

我在以前的尝试中犯了一个错误...

这是错误的... 工具应该是tool() ...

class Garage(HasTraits):
    things = TraitList(Tool, item_validator=garage_item_validator)

这是正确的...

class Garage(HasTraits):
    things = TraitList(Tool(), item_validator=garage_item_validator)

此外,我发现我可以手动分配self.things.item_validator初始化garage()

这也正确运行...

from traits.api import HasTraits, HasRequiredTraits, TraitList, TraitError
from traits.api import Regex, List, Enum, Either

def garage_item_validator(item):
    try:
        if isinstance(item, Tool):
            if item.adjective!="pink" and item.adjective!="floral":
                return item
            else:
                raise ValueError(item.adjective, item.name)
        else:
            raise ValueError(item.adjective, item.name)
    except Exception:
        raise ValueError(item.adjective, item.name)

class Tool(HasRequiredTraits):
    name = Regex(regex=r"[Ww]rench|[Ll]awnmower", required=True)
    adjective = Enum(*["brown", "rusty", "pink", "floral"])

    def __init__(self, name=None, adjective=None):
        self.name = name
        self.adjective = adjective

    def __repr__(self):
        return """<Tool: {}>""".format(self.name)

class Garage(HasTraits):
    things = TraitList(Tool())

    def __init__(self):
        self.things = list()
        self.things.item_validator = garage_item_validator
    #   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    #   Manually assign item_validator attr **after initializing self.things**

if __name__=="__main__":
    my_garage = Garage()
    my_garage.things.append(Tool("Lawnmower", "brown"))

    # now, pink correctly fails in garage_item_validator()
    my_garage.things.append(Tool("wrench", "pink")) # <-- pink fails

    print(my_garage)

I made a mistake in my previous attempts...

This is wrong... Tool should be Tool()...

class Garage(HasTraits):
    things = TraitList(Tool, item_validator=garage_item_validator)

This is correct...

class Garage(HasTraits):
    things = TraitList(Tool(), item_validator=garage_item_validator)

Additionally, I found that I can manually assign self.things.item_validator after initializing the Garage()

This also runs correctly...

from traits.api import HasTraits, HasRequiredTraits, TraitList, TraitError
from traits.api import Regex, List, Enum, Either

def garage_item_validator(item):
    try:
        if isinstance(item, Tool):
            if item.adjective!="pink" and item.adjective!="floral":
                return item
            else:
                raise ValueError(item.adjective, item.name)
        else:
            raise ValueError(item.adjective, item.name)
    except Exception:
        raise ValueError(item.adjective, item.name)

class Tool(HasRequiredTraits):
    name = Regex(regex=r"[Ww]rench|[Ll]awnmower", required=True)
    adjective = Enum(*["brown", "rusty", "pink", "floral"])

    def __init__(self, name=None, adjective=None):
        self.name = name
        self.adjective = adjective

    def __repr__(self):
        return """<Tool: {}>""".format(self.name)

class Garage(HasTraits):
    things = TraitList(Tool())

    def __init__(self):
        self.things = list()
        self.things.item_validator = garage_item_validator
    #   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    #   Manually assign item_validator attr **after initializing self.things**

if __name__=="__main__":
    my_garage = Garage()
    my_garage.things.append(Tool("Lawnmower", "brown"))

    # now, pink correctly fails in garage_item_validator()
    my_garage.things.append(Tool("wrench", "pink")) # <-- pink fails

    print(my_garage)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文