- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我被告知要在我的公司设计一个新的 API,而在编码实践方面我面临着两难境地。
我的 API 在运行之前必须进行多次检查,并且通常需要多个级别的函数才能运行。
一切都很好,直到这里。但是我的大部分检查(sub to sub to sub)函数都需要主 API 返回,而不做任何事情。几乎我所有的检查函数都必须返回一些数据,这些数据将被下一个检查函数使用,这就是我的问题所在。由于这种结构,我必须在每个检查函数结束时将状态与处理后的数据一起返回,并且在调用该函数后,我必须在进入下一个函数之前检查状态。
示例代码:
def check1a():
if some_process():
return True, data_positive
return False, data_negative
#data_positive and data_negative cannot be used to identify whether the check passed or not.
def check1():
stats,data = check1a()
if not status:
return False, data
status, data = check1b(data)
if not status:
return False, data
status, data = check1c(data)
if not status:
return False, data
return status, data
def mainAPI():
status, data = check1(data)
if not status:
return data
status, data = check2(data)
if not status:
return data
status, data = check3()
if not status:
return "Failed"
return data
作为“DRY”概念的虔诚追随者,如果觉得使用异常以以下方式运行代码将是最好的。
def check1a():
if some_process():
return data_positive
exception1a = Exception("Error in check 1 a")
exception.data = data_negative
raise exception
def check1():
data = check1a()
data = check1b(data)
data = check1c(data)
return data
def mainAPI():
try:
data = check1(data)
data = check2(data)
data = check3(data)
return data
except Exception as e:
return e.data #I know exceptions don't always have data, but this is an illustration of what I think I should implement
不幸的是,在代码中引发异常来实现这种工作在我的公司有点回避。
所以这是我的问题。
最佳答案
这可能不是一个很好的答案,其他人可以提供更多帮助。
这是一个基于意见的话题。如果他们说他们不喜欢你像这样使用try exception
,那么他们可能不相信“请求宽恕比许可更好”原则。
话虽这么说,抛出一个Exception
并不坏;但是捕获一般的 Exception
被认为是不好的。如果某个软件未按预期运行(即以某种未知方式),您希望它失败,因此您应该只捕获您想要捕获的特定 Exception
。
您可以在这里找到大量可行的异常,只需选择一个看起来合理的并使用它:Python Programming Exceptions
如果您不想捕获先前存在的异常之一,您可以随时创建自己的异常:
class MyAPIException(Exception):
def __init___(self, val):
self.val = val
Exception.__init__(self, "APIException with with arguments {0}".format(self.val))
def do_stuff(a,b,c):
raise MyAPIException({
'a' : a,
'b' : b,
'c' : c,
})
try:
do_stuff(1, 2, 3)
except MyAPIException as e:
print("API Exception:", e)
另一种可以帮助DRY 的方法是使用列表来调用电话。
def check1():
# List of functions you want to call in order
calls = [check1a, check1b, check1c]
for index, call in enumerate(calls):
# If it is the first function we will not pass any data
status, data = call() if index == 0 else call(data)
if not status:
return False, data
return status, data
如果您想返回每个函数调用的结果,此实现还可以轻松地将其实现为生成器。
关于python - 如何将控件导航到顶级功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55677032/
我有一个用于重构练习的测试套件,我希望它与 Data.List 兼容。和 Data.List.NonEmpty .练习包含一个函数 foo :: [Foo] -> Foo并且测试套件有一些 data
我下载了 Android Studio 项目。项目根目录只有一个 build.gradle 具有下一个结构: apply plugin: 'com.android.library' android {
我似乎无法正确使用最接近的函数来删除删除按钮之外的 UL。 HTML X Data Jquery $('.remove').click(function(){
我需要一些帮助。顶级 Activity 必须位于自定义包之外吗?这样的话,MainActivity应该是在自定义包之外吧?Activity 是自定义包名称。
我正在尝试使用 AppCompat Drawer 和多个顶级 Activity (不是 fragment )创建一个基本应用程序 - 我不太清楚如何管理后台堆栈 - 我已经尝试了大约一百种不同的方法-
无法找到任何相关信息,我可以自己完成此操作,但我觉得将其保留在查询中可能是最好的选择(如果可能的话)。 基本上我想尝试添加查询的顶级“统计”部分。 所以当我得到结果时,我会看到这样的结果 num_ro
我在菜单中遇到 onmouseout/over delay 问题。我发现通过将 setTimeout 数字从 100 更改为 2000,它延迟了顶层菜单的隐藏而不是子级菜单,并且在新的 onmouse
是否有一个选项可以通过使用动画在运行时简单地 move Tkinter TopLevel() 窗口?我想到了由按钮触发的平滑 moveInAnimation。 这是一些代码片段: from Tkint
我从正在调用的 API 中获取了以下 JSON 文件: { "14500": [ { "5": { "versionName": "VersionOne",
在 C++ Primer 第五版中,它说: int f(int){ /* can write to parameter */} int f(const int){ /* cannot write to
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我正在使用 Retrofit 进行一些 API 调用。对于特定端点,返回的 json 看起来有点像这样: 端点:api.example.com/1.0/userinfo?userid=7 返回的响应看
假设我有一个 Python 文件,我想在顶层运行它,但在它完成后,我想从它停止的地方继续。我希望能够使用它创建的对象等。 一个简单的例子,假设我有一个执行 i = 5 的 Python 脚本。当脚本结
我在 Xcode 中使用 Cmake 来生成一个 c++/c“项目”(my_project) 和一些 c++/c“目标”(一个是二进制文件,其余是库) 我的 CMakeLists.txt 看起来像这样
我正在尝试学习 redux,但遇到了错误。我只有两个文件,一个 index.html 文件和一个 main.js 文件,html 文件中有指向 jquery 和 redux cdns 的链接。我在 r
我有这个通用函数可以保存在 NSUserDefaults 中,通常可以正常工作,但现在我想保存一个 boolean 值,但出现错误。我找不到任何东西,我不明白为什么它不起作用。 extension U
我正在使用 Retrofit 进行 API 调用。顶级对象的命名取决于请求的查询参数。例如,这样的请求: api.somewebsite.com/1.0/mix_info?mix_id=69 返回这样
我做汤: from bs4 import BeautifulSoup soup = BeautifulSoup("My paragraph My link", "html.parser") 我想删除第
看完new egghead course由 Dan Abramov 撰写,我对提到的选择器有疑问。 选择器的目的是向组件隐藏状态树的详细信息,以便在树发生变化时便于以后管理代码。 如果我理解正确,那意
我是一名优秀的程序员,十分优秀!