如何stdout抑制输出?
例如,分号可用于抑制返回对象的显示
>>> 1+1
2
>>> 1+1; # No output!
Run Code Online (Sandbox Code Playgroud)
但是,打印到stdout的函数不受分号影响.
>>> print('Hello!')
Hello!
>>> MyFunction()
Calculating values...
Run Code Online (Sandbox Code Playgroud)
如何才能从输出print/ MyFunction被抑制?
给出一个数据帧字典,如:
dict = {'ABC': df1, 'XYZ' : df2} # of any length...
Run Code Online (Sandbox Code Playgroud)
其中每个数据框具有相同的列和相似的索引,例如:
data Open High Low Close Volume
Date
2002-01-17 0.18077 0.18800 0.16993 0.18439 1720833
2002-01-18 0.18439 0.21331 0.18077 0.19523 2027866
2002-01-21 0.19523 0.20970 0.19162 0.20608 771149
Run Code Online (Sandbox Code Playgroud)
将所有数据帧合并为一个的最简单方法是什么,使用多索引,如:
symbol ABC XYZ
data Open High Low Close Volume Open ...
Date
2002-01-17 0.18077 0.18800 0.16993 0.18439 1720833 ...
2002-01-18 0.18439 0.21331 0.18077 0.19523 2027866 ...
2002-01-21 0.19523 0.20970 0.19162 0.20608 771149 ...
Run Code Online (Sandbox Code Playgroud)
我已经尝试了一些方法 - 例如,对于每个数据帧,用多索引替换列.from_product(['ABC', columns])然后连接axis=1,但没有成功.
默认情况下,CMake输出没有分隔符的列表,例如
set(my_list a b c d)
message(${my_list})
Run Code Online (Sandbox Code Playgroud)
输出
abcd
Run Code Online (Sandbox Code Playgroud)
你怎么能(轻松地)使CMake输出像实际存储的那样?
a;b;c;d
Run Code Online (Sandbox Code Playgroud)
(典型的用例是输出搜索路径列表)
如果可能的话,你如何模拟在单元测试中触发升压定时器的时间?
例如,是否可以实现以下内容:
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
void print(const boost::system::error_code& /*e*/)
{
std::cout << "Hello, world!\n";
}
int main()
{
boost::asio::io_service io; // Possibly another class needed here, or a way of setting the clock to be fake
boost::asio::deadline_timer t(io, boost::posix_time::hours(24));
t.async_wait(&print);
io.poll(); // Nothing should happen - no handlers ready
// PSEUDO-CODE below of what I'd like to happen, jump ahead 24 hours
io.set_time(io.get_time() + boost::posix_time::hours(24));
io.poll(); // The timer should go off
return 0;
}
Run Code Online (Sandbox Code Playgroud)
更新感谢您的所有答案,他们提供了对问题的深入见解.我提供了自己的答案(SSCCE),但没有提供的帮助就无法做到.
我是Sourceforge和SVN(Windows上的SilkSVN)的新手.
我设法检查了一个项目
svn co --username=zeroth123 svn://zeroth123@svn.code.sf.net/p/stddecimal/code/trunk stddecimal-code
Run Code Online (Sandbox Code Playgroud)
但是,每次我尝试使用ssh(所以我可以检查一下)它都失败了
> svn co --username=zeroth123 svn+ssh://zeroth123@svn.code.sf.net/p/stddecimal/code/trunk stddecimal-code
svn: E720087: Unable to connect to a repository at URL 'svn+ssh://zeroth123@svn.code.sf.net/p/stddecimal/code/trunk'
svn: E720087: Can't create tunnel: The parameter is incorrect.
Run Code Online (Sandbox Code Playgroud)
我已经SVN_SSH指出我的完全合格plink.exe,它没有任何区别.我也试过改变它junk.exe并没有得到任何不同,所以我认为我们甚至没有看SVN_SSH,这耗尽了我在Sourceforge和其他论坛上找到的所有建议.
为了记录我是项目管理员,因此有写权限(或者至少我认为!)
有任何想法吗?
我正在查看具有以下if-test 的第三方库:
if isinstance(xx_, numpy.ndarray) and xx_.dtype is numpy.float64 and xx_.flags.contiguous:
xx_[:] = ctypes.cast(xx_.ctypes._as_parameter_,ctypes.POINTER(ctypes.c_double))
Run Code Online (Sandbox Code Playgroud)
似乎xx_.dtype is numpy.float64总是失败:
>>> xx_ = numpy.zeros(8, dtype=numpy.float64)
>>> xx_.dtype is numpy.float64
False
Run Code Online (Sandbox Code Playgroud)
测试dtypenumpy数组的正确方法是float64什么?
更新:我不是在询问可变数量的参数.最终结果(如果可能的话)应该是一个完全按照我下面的例子定义的函数,或者至少完全按照这种方式行事 - 我在问题中添加了一些例子来试图澄清这一点.
(可能的情况是,实现这一目标的唯一方法是使用
__init__然后手动处理这些并在输入与预期不完全匹配时引发适当的异常,在这种情况下,这将是一个有效的答案)
为了我自己的娱乐,我想知道如何实现以下目标:
functionA = make_fun(['paramA', 'paramB'])
functionB = make_fun(['arg1', 'arg2', 'arg3'])
Run Code Online (Sandbox Code Playgroud)
相当于
def functionA(paramA, paramB):
print(paramA)
print(paramB)
def functionB(arg1, arg2, arg3):
print(arg1)
print(arg2)
print(arg3)
Run Code Online (Sandbox Code Playgroud)
这意味着需要以下行为:
functionA(3, paramB=1) # Works
functionA(3, 2, 1) # Fails
functionB(0) # Fails
Run Code Online (Sandbox Code Playgroud)
问题的焦点在于变量argspec - 我很舒服地使用通常的装饰器技术创建函数体.
对于那些感兴趣的人,我正在尝试以编程方式创建类似以下的类.同样困难在于__init__使用编程参数生成方法 - 类的其余部分使用装饰器或可能是元类看起来很简单.
class MyClass:
def __init__(self, paramA=None, paramB=None):
self._attr = ['paramA', 'paramB']
for a in self._attr:
self.__setattr__(a, None)
def __str__(self):
return str({k:v for (k,v) in self.__dict__.items() if k in …Run Code Online (Sandbox Code Playgroud) 题
什么时候需要asio_handler_invoke通过简单地包装处理程序来实现无法实现的功能?
演示需要的情况的典型示例asio_handler_invoke将是理想的.
背景
boost asio docs包含了一个如何在asio_handler_invoke 这里使用的例子,但我认为它不是一个令人信服的例子,说明为什么要使用调用处理程序.在该示例中,您似乎可以进行如下更改(并删除asio_handler_invoke)并获得相同的结果:
template <typename Arg1>
void operator()(Arg1 arg1)
{
queue_.add(priority_, std::bind(handler_, arg1));
}
Run Code Online (Sandbox Code Playgroud)
同样,在我关于处理程序跟踪的答案中asio_handler_invoke,尽管Tanner Sansbury的回答建议使用调用挂钩作为解决方案,但同样似乎没有必要使用它.
boost用户组上的这个线程提供了更多信息 - 但我不明白其意义.
从我所看到的,它似乎asio_handler_invoke总是被称为asio_handler_invoke(h, &h),似乎没有多大意义.在什么情况下,参数不是(基本上)相同对象的副本?
最后一点 - 我只是io_service::run()从一个线程调用,所以可能是我遗漏了一些来自多线程循环经验的明显东西.
在尝试使用不可复制(私有拷贝构造函数)但可移动对象时,我得到了编译错误,g++ (GCC) 4.7.2但没有.对我来说,我的例子看起来与SO和其他地方的许多其他例子相同.错误消息使得它看起来像结构不是"可直接构造"的问题 - 我不知道这意味着什么所以我更加不确定为什么一个对象需要被"直接构造"才能被推回.MSVC-2012std::vector::push_back
#include <vector>
#include <memory>
struct MyStruct
{
MyStruct(std::unique_ptr<int> p);
MyStruct(MyStruct&& other);
MyStruct& operator=(MyStruct&& other);
std::unique_ptr<int> mP;
private:
// Non-copyable
MyStruct(const MyStruct&);
MyStruct& operator=(const MyStruct& other);
};
int main()
{
MyStruct s(std::unique_ptr<int>(new int(5)));
std::vector<MyStruct> v;
auto other = std::move(s); // Test it is moveable
v.push_back(std::move(other)); // Fails to compile
return 0;
}
Run Code Online (Sandbox Code Playgroud)
给出错误
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/type_traits: In instantiation of ‘struct std::__is_direct_constructible_impl<MyStruct, const MyStruct&>’:
... snip ...
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/stl_vector.h:900:9: required from ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with …Run Code Online (Sandbox Code Playgroud) 有没有办法输入一个对象实现两个不同接口的提示?也许类似于my_function下面的假例子。
class SomeLibraryClass(InterfaceA, InterfaceB):
...
def my_function(obj: Multiple[InterfaceA, InterfaceB]):
obj.do_interface_a_stuff()
obj.do_interface_b_stuff()
Run Code Online (Sandbox Code Playgroud)
我无法修改SomeLibraryClass。我可以定义,class AB(InterfaceA, InterfaceB)但我使用的类型检查工具仍然会标记SomeLibraryClass为未实现AB。该解决方案还导致定义我们想要键入提示的所有接口组合的类的激增。(这可能是做事的“正确方法”,在这种情况下这就是答案!)
c++ ×3
python ×3
boost-asio ×2
python-3.x ×2
c++11 ×1
cmake ×1
g++ ×1
multi-index ×1
numpy ×1
pandas ×1
sourceforge ×1
svn ×1
unit-testing ×1
windows ×1