使用未实例化的类作为命名空间来组织对象? [英] Organize objects using un-instantiated classes as namespaces?
问题描述
我构建了一个模块来保存所有常见的 selenium expected_conditions,然后在各种测试中引用这些条件.(这个例子的硒部分并不重要,重要的是我有一个包含几千个项目的目录".)
I've built a module to hold all of the common selenium expected_conditions which are then referenced throughout various tests. (The selenium portion of this example is not important, just the fact that I have a 'catalog' of a few thousand items.)
# ec module
class EcHR(object):
class sidemenu(object):
hrfile: Clickable = Clickable((By.ID, "sidemenu_HRFile"), f"HR File")
hrvistainfo: Clickable = Clickable((By.ID, "sidemenu_HRVistA Info"), f"HR VistA Info")
class hrfile(__hr_search__):
class actionmenu(__hr_search__.actionmenu):
add: Clickable = Clickable((By.ID, "NewHireLink"), f"Add {BUTTON}")
personnel: Clickable = Clickable((By.ID, 'setEmpType'), f"Personnel Filter {DROPDOWN}")
status: Clickable = Clickable((By.ID, 'setStatus'), f"Status {DROPDOWN}")
configure: Clickable = Clickable((By.ID, "configureLinkGen"), f"Configure {BUTTON}")
reports: Clickable = Clickable((By.ID, 'Reports'), f"Reports {BUTTON}")
class addmenu(object):
employee: Clickable = Clickable((By.ID, f'Employee'), f"Add->Employee {BUTTON}")
对 ec 模块中给定项目的示例引用.
Sample reference to a given item in the ec module.
import ec
# navigation helper method for a given page
def do_hrfile_nav(self):
self.clickbutton(ec.EcHR.sidemenu.hrfile)
这很好用;我可以跟踪每个对象的使用情况,同时仍然为对象提供可读性上下文.
This works great; I can trace the usage of each object while still providing readability context for what the object is.
self.clickbutton(ec.EcHR.sidemenu.hrfile)
告诉读者我们正在点击 HR
上的侧菜单项 hrfile
页.这对于区分 ec.EcHR.hrfile
很重要,后者是一组不同的对象.
self.clickbutton(ec.EcHR.sidemenu.hrfile)
tells the reader we are clicking on the sidemenu item hrfile
on the HR
page. This is important to distinguish from the ec.EcHR.hrfile
which is a different set of objects.
我的问题:是否有更好/更简洁的方式来组织这些对象?我正在使用从未被实例化的类.(不知何故,由于我无法表达的原因而感到尴尬)这些类仅作为一种以有组织的方式引用它们的手段.也许某种具有类结构的轻量级数据对象?(我是不是想多了?)
My question: Is there a better/cleaner way to organize these objects? I'm using classes that never get instantiated. (which somehow feels awkward for reasons I can't articulate) These classes act solely as a means to reference them in an organized way. Perhaps some sort of lightweight data object that has class-like structure? (am I over thinking things?)
推荐答案
我有一些想法.可能有用.
I've got some ideas. It may be useful.
如何使用函数作为命名空间?
How about using functions as namespaces?
@namespace
def EcHR():
@namespace
def sidemenu():
hrfile: Clickable = Clickable((By.ID, "sidemenu_HRFile"), f"HR File")
return locals()
@namespace
def hrfile():
@namespace
def actionmenu():
add: Clickable = Clickable((By.ID, "NewHireLink"), "Add {BUTTON}")
@namespace
def addmenu():
employee: Clickable = Clickable(
(By.ID, f"Employee"), "Add->Employee {BUTTON}"
)
return locals()
return locals()
return locals()
return locals()
我已将每个 class
更改为一个函数.每个函数都必须用 namespace
装饰器装饰,并且必须返回 locals()
或任何带有自定义键、val 映射的 dict
.
I've changed every class
to a function. And every function must be decorated with namespace
decorator and must return locals()
or any dict
with custom key, val mapping.
这是 NameSpace
类和 namespace
装饰器.
Here's NameSpace
class and namespace
decorator.
from functools import reduce
class NameSpace:
def __init__(self, name):
self.__name__ = name
def __str__(self):
return "{}".format(self.__name__)
def __repr__(self):
return "NameSpace({})".format(self.__name__)
def __call__(self):
pass
def namespace(func):
return reduce(
lambda ns, kv: setattr(ns, kv[0], kv[1]) or ns,
func().items(),
NameSpace(func.__name__),
)
namespace
装饰器运行装饰函数并将返回的 dict
转换为 NameSpace
实例.
The namespace
decorator runs the decorated function and transforms the returned dict
into a NameSpace
instance.
如何访问对象?
print(EcHR.sidemenu.hrfile)
print(EcHR.hrfile.actionmenu.addmenu.employee)
这篇关于使用未实例化的类作为命名空间来组织对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!