Python-如何将实例变量值传递给描述符参数? [英] Python - How to pass instance variable value to descriptor parameter?
问题描述
考虑以下情况,
ItemList类:
# filename: item_list.py
# locators dictionary is intentionally placed outside `ItemList` class
locators = {
'item_id': 'css=tr:nth-of-type({item_num}) .id',
'item_title': 'css=tr:nth-of-type({item_num}) .alert-rules',
# other properties
}
class ItemList(Item):
# --- these are data descriptors ---
# getting item_id value based on its item_num
item_id = TextReadOnly(locators['item_id'].format(item_num=self.item_num))
# getting item_title value based on its item_num
item_title = TextReadOnly(locators['item_title'].format(item_num=self.item_num))
def __init__(self, context, item_num=0):
# self.item_num can't be passed to locators
self.item_num = item_num
super(ItemList, self).__init__(
context
)
在这种情况下,我想将self.item_num
值传递给ItemList
类中的定位器字典内的item_num
.
In this case, I want to pass self.item_num
value to item_num
inside locators dictionary in ItemList
class.
原因是我希望每个item_id
和item_title
都引用特定的item_num
.
The reason is I want each of item_id
and item_title
refers to particular item_num
.
我被困在这部分上:
item_id = TextReadOnly(locators['item_id'].format(item_num=self.item_num))
item_title = TextReadOnly(locators['item_title'].format(item_num=self.item_num))
已经创建实例后,
self.item_num
值不能传递给定位器.
self.item_num
value can't be passed to locators when instance is already made.
实施方式:
# filename: test.py
# print item_id values from item_num=1
item_1 = ItemList(context, 1)
print (‘Item_id_1: ’ + item_1.id)
# print item_id values from item_num=2
item_2 = ItemList(context, 2)
print (‘Item_id_2: ’ + item_2.id)
# ^ no result produced from item_1.id and item_2.id because self.item_num value can't be passed to locators
是否可以从实例变量更新数据描述符?
Is it possible to update data descriptor from instance variable?
如何将实例变量值传递给数据描述符参数?
How to pass instance variable value to data descriptor parameter?
其他信息:
我尝试将item_id
和item_title
称为实例属性,但是没有运气.我注意到这篇文章中的数据描述符不能是实例属性:数据描述符应该是类属性
I tried to call item_id
and item_title
as instance attribute but no luck. I noticed that data descriptor can’t be instance attribute from this post: Data descriptor should be a class attribute
物品类别:
# filename: item.py
import abc
class Item(Page):
__metaclass__ = abc.ABCMeta
def __init__(self, context):
self.__context = context
super(Item, self).__init__(
self.__context.browser
)
描述符:
# filename: text_read_only.py
class TextReadOnly(object):
def __init__(self, locator):
self.__locator = locator
def __get__(self, instance, owner=None):
try:
e = instance.find_element_by_locator(self.__locator)
except AttributeError:
e = instance.browser.find_element_by_locator(self.__locator)
return e.text
def __set__(self, instance, value):
pass
推荐答案
我发现此,并提出了将描述符添加到对象的解决方案.
I found this article and came up with solution to add the descriptors to an object.
下面的解决方案对我有用:
This solution below worked for me:
-
将
__getattribute__
和__setattr__
添加到物品类别
# filename: item.py
import abc
class Item(Page):
__metaclass__ = abc.ABCMeta
def __init__(self, context):
self.__context = context
super(Item, self).__init__(
self.__context.browser
)
def __getattribute__(self, name):
value = object.__getattribute__(self, name)
if hasattr(value, '__get__'):
value = value.__get__(self, self.__class__)
return value
def __setattr__(self, name, value):
try:
obj = object.__getattribute__(self, name)
except AttributeError:
pass
else:
if hasattr(obj, '__set__'):
return obj.__set__(self, value)
return object.__setattr__(self, name, value)
将类属性移动到 ItemList 构造函数中的实例属性
Move class attribute to instance attribute in ItemList constructor
# filename: item_list.py
# locators dictionary is intentionally placed outside `ItemList` class
locators = {
'item_id': 'css=tr:nth-of-type({item_num}) .id',
'item_title': 'css=tr:nth-of-type({item_num}) .alert-rules',
# other properties
}
class ItemList(Item):
def __init__(self, context, item_num=0):
self.item_num = item_num
# self.item_num is able to be passed to locators
self.item_id = TextReadOnly(locators['item_id'].format(item_num=self.item_num))
self.item_title = TextReadOnly(locators['item_title'].format(item_num=self.item_num))
super(ItemList, self).__init__(
context
)
这篇关于Python-如何将实例变量值传递给描述符参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!