在Python中,我可以执行"x in list"以查看列表是否包含x.Scheme中有没有相同的内置功能可以做到这一点?
Ruby如何nil在代码中体现?例如,在Python你可能会当它是指另一个说法没有使用一个默认的说法,但在Ruby中,你可以参考其他参数的ARG列表(见这个问题).在JS中,undefined弹出更多,因为你根本无法指定默认参数.你能举例说明如何RubyNone弹出以及它是如何处理的?
我不是只想用一个例子nil.优选地,它将是nil由于某种原因必须使用的真实代码片段.
有没有一种简单的方法来请求GTK小部件具有最小宽度/高度?我知道你可以在a的列上做到这一点TreeView,但它是否适用于一般小部件?
假设你写了一个类Sup,我决定将它扩展为Sub< Sup.我不仅需要了解您发布的界面,还需要了解您的私人领域.见证这个失败:
class Sup
def initialize
@privateField = "from sup"
end
def getX
return @privateField
end
end
class Sub < Sup
def initialize
super()
@privateField = "i really hope Sup does not use this field"
end
end
obj = Sub.new
print obj.getX # prints "i really hope Sup does not use this field"
Run Code Online (Sandbox Code Playgroud)
问题是,解决这个问题的正确方法是什么?似乎子类应该能够使用它想要的任何字段而不会弄乱超类.
我想要一个多色选择小部件.我这样做的方式是使用"+"按钮和最初为空的vbox.当按下+时,它会向包含" - "按钮和3个旋转框的vbox添加一个QHBoxLayout.当按下" - "按钮时,我希望该行消失,并且在添加该行之前,所有内容都会恢复原样.我目前的代码是:
vbox = self.ui.color_layout #from QtDesigner
hbox = QtGui.QHBoxLayout()
remove = QtGui.QPushButton("-", parent=self)
remove.clicked.connect(lambda: vbox.removeItem(hbox))
rspin = QtGui.QSpinBox(parent=self)
gspin = QtGui.QSpinBox(parent=self)
bspin = QtGui.QSpinBox(parent=self)
hbox.addWidget(remove)
hbox.addWidget(QtGui.QLabel("R:", parent=self))
hbox.addWidget(rspin)
hbox.addWidget(QtGui.QLabel("G:", parent=self))
hbox.addWidget(gspin)
hbox.addWidget(QtGui.QLabel("B:", parent=self))
hbox.addWidget(bspin)
vbox.addLayout(hbox)
Run Code Online (Sandbox Code Playgroud)
添加小部件工作正常.然而,删除它们导致一个看起来很乱的东西,其中行实际上没有被删除,但是间距都搞砸了.
我究竟做错了什么?
编辑:文档说,为removeWidget:
在此调用之后,调用者有责任为窗口小部件提供合理的几何图形或将窗口小部件放回布局中.
我怎么做?(我来自GTK背景......)
编辑2:我甚至跟踪行并调用takeAt函数来删除它,但它仍然搞砸了.是什么赋予了?看起来布局被删除但是没有一个小部件......
编辑3:这也行不通,只是以类似的方式搞砸了:
vbox = self.ui.color_layout
hbox = QtGui.QHBoxLayout()
row_widget = QtGui.QWidget(parent=self) #dummy widget to hold this stuff
remove = QtGui.QPushButton("-", parent=self)
def remove_func():
vbox.removeWidget(row_widget)
remove.clicked.connect(remove_func)
rspin = QtGui.QSpinBox(parent=self)
gspin = QtGui.QSpinBox(parent=self)
bspin …Run Code Online (Sandbox Code Playgroud) 在制作网站时测试发送POST请求的简单方法是什么?我可以通过在URL(example.com/?foo=bar&bar=baz)中输入GET请求来轻松发送GET请求,但是发送POST请求的方法是多么简单?
我有一个tmp_drop_ids包含一列的表id,以及330万个条目.我想迭代表,每200个条目做一些事情.我有这个代码:
LIMIT = 200
for offset in xrange(0, drop_count+LIMIT, LIMIT):
print "Making tmp table with ids %s to %s/%s" % (offset, offset+LIMIT, drop_count)
query = """DROP TABLE IF EXISTS tmp_cur_drop_ids; CREATE TABLE tmp_cur_drop_ids AS
SELECT id FROM tmp_drop_ids ORDER BY id OFFSET %s LIMIT %s;""" % (offset, LIMIT)
cursor.execute(query)
Run Code Online (Sandbox Code Playgroud)
一开始运行良好(〜0.15s生成tmp表),但它会偶尔减速,例如大约300k票据开始需要11-12秒生成这个tmp表,再次大约400k.它基本上似乎不可靠.
我将在其他查询中使用这些ID,因此我认为将它们放在tmp表中的最佳位置.有没有更好的方法来迭代这样的结果?
考虑:
int convert_it(std::string& x)
{
return 5;
}
void takes_int_ref(int& i)
{
}
Run Code Online (Sandbox Code Playgroud)
我想编写一个函数,只有在convert_it可以应用并传递结果时才存在takes_int_ref.也就是说,功能体是:
template <typename A>
void doit(A& a)
{
int i = convert_it(a);
takes_int_ref(i);
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我这样做:
template <typename A>
auto doit(A& a) -> decltype(takes_int_ref(convert_it(a)), void())
Run Code Online (Sandbox Code Playgroud)
它不起作用,因为invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'.
我想到了以下解决方案,它有效:
template <typename T>
T& gimme_ref(T t) { throw std::runtime_error("No"); return t; }
template <typename A>
auto doit(A& a) -> decltype(takes_int_ref(gimme_ref(convert_it(a))), void())
Run Code Online (Sandbox Code Playgroud)
然而,它似乎是hackish, …
我想创建一个宏,将一对解包为两个局部变量.如果它只是一个变量,我想不创建该对的副本,这将实现:
#define UNPACK_PAIR(V1, V2, PAIR) \
auto& V1 = PAIR.first; \
auto& V2 = PAIR.second;
UNPACK_PAIR(one, two, x);
Run Code Online (Sandbox Code Playgroud)
但是,我也希望它不要评估多次给出的表达式,例如这应该只调用expensive_computation()一次:
UNPACK_PAIR(one, two, expensive_computation());
Run Code Online (Sandbox Code Playgroud)
如果我做:
#define UNPACK_PAIR_A(V1, V2, PAIR) \
auto tmp = PAIR; \
auto& V1 = tmp.first; \
auto& V2 = tmp.second;
Run Code Online (Sandbox Code Playgroud)
然后它适用于expensive_computation()案例,但它在x案件中复制.如果我做:
#define UNPACK_PAIR_R(V1, V2, PAIR) \
auto& tmp = PAIR; \
auto& V1 = tmp.first; \
auto& V2 = tmp.second;
Run Code Online (Sandbox Code Playgroud)
然后它在x没有复制的情况下工作,但在这种expensive_computation()情况下失败.如果我做:
#define UNPACK_PAIR_CR(V1, V2, PAIR) \
const auto& …Run Code Online (Sandbox Code Playgroud) python ×4
c++ ×3
c++11 ×2
ruby ×2
c ×1
decltype ×1
file ×1
file-format ×1
gtk ×1
iso ×1
javascript ×1
limit ×1
list ×1
macros ×1
null ×1
offset ×1
oop ×1
performance ×1
post ×1
postgresql ×1
pygtk ×1
pyqt ×1
pyqt4 ×1
qt ×1
qt4 ×1
scheme ×1
sql ×1
subclass ×1
subclassing ×1
templates ×1
testing ×1