- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在学习 urwid。
Urwid 列表框有一个不适合我的 API。例如,为了将焦点更改为下一个/上一个元素,我想写:
listbox.focus_next() / listbox.focus_previous()
但是urwid.ListBox提供的API是这样的:
1) 关注列表框中的前一个元素
listwalker = listbox.body
widget,current_position = listwalker.get_focus()
try :
widget,previous_position = listwalker.get_prev(current_position)
listwalker.set_focus(previous_position)
except :
# you're at the beginning of the listbox
pass
2) 关注列表框中的下一个元素
# same code, except that you change get_prev with get_next
listwalker = listbox.body
widget,current_position = listwalker.get_focus()
try :
widget,next_position = listwalker.get_next(current_position)
listwalker.set_focus(next_position)
except :
# you're at the end of the listbox
pass
请注意,所有这些方法都不是在列表框本身上调用的,而是在其属性之一(主体)上调用的。
对这种情况不满意,我决定对列表框本身进行子类化,为 API 提供两个新服务(方法):focus_previous() 和 focus_next(),如下所示:
class MyListBox(urwid.ListBox):
def focus_next(self):
try:
self.body.set_focus(self.body.get_next(self.body.get_focus()[1])[1])
except:
pass
def focus_previous(self):
try:
self.body.set_focus(self.body.get_prev(self.body.get_focus()[1])[1])
except:
pass
在处理令人不快的 API 时,这是(子类化)正确的方法吗?
最佳答案
只要 MyListBox
仍然能够站在常规 ListBox
所在的位置,子类化就应该是安全的。毕竟,MyListBox
确实是一个ListBox
,只是一个特殊的。
Urwid documentation其本身似乎也同意:
Here we are customizing the Filler decoration widget that is holding our Edit widget by subclassing it and defining a new keypress() method. Customizing decoration or container widgets to handle input this way is a common pattern in Urwid applications. This pattern is easier to maintain and extend than handling all special input in an unhandled_input function.
如果您发现自己有太多空闲时间,您可能还想阅读Composition Instead Of Inheritance在原始 Wiki 上,它可能给出了太多关于哪种情况最好的意见。
关于python - 如果您对 API 不满意该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19465847/
在 Spring Boot 应用程序(v. 1.3.2)中,我尝试使用 jOOQ 生成所有映射数据库表的 POJO,以便它们也被注释为与 JPA 一起使用。数据库是 PostgreSQL。 问题在于
我有一些非常标准的代码,它从一个流中获取一个序列化对象,它基本上看起来像这样: Object getObjectFromStream(InputStream is) { ObjectInpu
在我的 Node.js 代码上运行 JSLint 时,它显示 “'Uint8Array' was used before it was defined 。” Mozilla reference将其置于
我在 Xcode6、beta 6 左右创建了一个项目(今天升级到 b7)。使用 Swift,一直在为 iOS 8 SDK 等开发。 到目前为止,Storyboard 有两个 View Controll
我是一名优秀的程序员,十分优秀!