我想使用boost属性树write_json序列化,这样可以节省一切为字符串,这并不是说数据是错误的,但我需要每次都明确地投下他们,我想别的地方使用它们.(比如在python或其他C++ json(非boost)库中)
这里是一些示例代码以及我根据区域设置得到的内容:
boost::property_tree::ptree root, arr, elem1, elem2;
elem1.put<int>("key0", 0);
elem1.put<bool>("key1", true);
elem2.put<float>("key2", 2.2f);
elem2.put<double>("key3", 3.3);
arr.push_back( std::make_pair("", elem1) );
arr.push_back( std::make_pair("", elem2) );
root.put_child("path1.path2", arr);
std::stringstream ss;
write_json(ss, root);
std::string my_string_to_send_somewhare_else = ss.str();
Run Code Online (Sandbox Code Playgroud)
并且my_string_to_send_somewhere_else是某事.像这样:
{
"path1" :
{
"path2" :
[
{
"key0" : "0",
"key1" : "true"
},
{
"key2" : "2.2",
"key3" : "3.3"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
反正是将它们保存为值,例如:
"key1" : true或"key2" : 2.2?
为什么不上升属性错误?函数对象没有任何比较方法.它以某种方式使用id()吗?
fun1 = lambda:x
fun2 = lambda:x
print fun1 == fun1 # True
print fun1 == fun2 # False
print fun1 > fun2 # True
print fun1 < fun2 # False
print fun1 > 1 # True
Run Code Online (Sandbox Code Playgroud)
我知道它比较了地址,但是怎么样?拦截__lt __,__ eq__等是否是一些低级别的黑客攻击?
我在每个函数上都有几个装饰器,有没有办法将它们打包成一个呢?
@fun1
@fun2
@fun3
def do_stuf():
pass
Run Code Online (Sandbox Code Playgroud)
改成:
@all_funs #runs fun1 fun2 and fun3, how should all_funs look like?
def do_stuf():
pass
Run Code Online (Sandbox Code Playgroud) class Wrapper(object):
def __init__(self, o):
# get wrapped object and do something with it
self.o = o
def fun(self, *args, **kwargs):
self = self.o # here want to swap
# or some low level C api like
# some_assign(self, self.o)
# so that it swaps id() mem addr to self.o
return self.fun(*args, **kwargs) # and now it's class A
class A(object):
def fun(self):
return 'A.fun'
a = A()
w = Wrapper(a)
print type(w) # wrapper
print w.fun() # some operation …Run Code Online (Sandbox Code Playgroud) 我有一个输入 df:
input_ = pd.DataFrame.from_records(
[
['X_val', 'Y_val1', 'Y_val2', 'Y_val3'],
[1, 10, 11, 31],
[2, 20, 12, 21],
[3, 30, 13, 11],])
Run Code Online (Sandbox Code Playgroud)
并且想要连接每个 y 值但仍然不同,用于绘图和分析的值来自哪里,我有多个具有可变数量的 Y 列的文件,最终将它们逐列连接起来并以相乘的值扩展,但想知道是否有是一个更好的解决方案,因为我的非常乏味。
expected_output_ = pd.DataFrame.from_records(
[
['X_val', 'Y_val' 'Y_type'],
[1, 10, 'Y_val1'],
[1, 11, 'Y_val2'],
[1, 31, 'Y_val3'],
[2, 20, 'Y_val1'],
[2, 12, 'Y_val2'],
[2, 21, 'Y_val3'],
[3, 30, 'Y_val1'],
[3, 13, 'Y_val2'],
[3, 11, 'Y_val3'],])
Run Code Online (Sandbox Code Playgroud)