基于此注释和引用的文档,Python 3.4+中的Pickle 4.0+应该能够腌制大于4 GB的字节对象.
但是,在Mac OS X 10.10.4上使用python 3.4.3或python 3.5.0b2时,我尝试挑选一个大字节数组时出错:
>>> import pickle
>>> x = bytearray(8 * 1000 * 1000 * 1000)
>>> fp = open("x.dat", "wb")
>>> pickle.dump(x, fp, protocol = 4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument
Run Code Online (Sandbox Code Playgroud)
我的代码中是否有错误或我误解了文档?
以下代码片段(在OS X上使用gcc 6.3.0使用-std = c ++ 17编译)演示了我的难题:
#include <experimental/tuple>
template <class... Ts>
auto p(Ts... args) {
return (... * args);
}
int main() {
auto q = [](auto... args) {
return (... * args);
};
p(1,2,3,4); // == 24
q(1,2,3,4); // == 24
auto tup = std::make_tuple(1,2,3,4);
std::experimental::apply(q, tup); // == 24
std::experimental::apply(p, tup); // error: no matching function for call to 'apply(<unresolved overloaded function type>, std::tuple<int, int, int, int>&)'
}
Run Code Online (Sandbox Code Playgroud)
为什么可以成功应用推断对lambda的调用而不是对模板函数的调用?这是预期的行为,如果是,为什么?
我有两个多态类型实例的Base*指针,我需要确定引用的对象是否相同.
我目前的方法是首先使用RTTI来检查类型是否相等.如果类型相等,我就调用一个虚拟的is_equivalent函数.
有更惯用的方法吗?
我有多个大文件(> 5M行数据),这些文件按唯一的时间戳排序.所有文件几乎包含所有相同的时间戳,除了少数随机丢失的行(<1000).我想有效地将所有文件中的数据加入到每个时间戳一行的单个数据集中,最好使用生成器.
除了丢失的行,我可以使用zip:
def get_data(list_of_iterables):
for data in zip(*list_of_iterables):
yield data
Run Code Online (Sandbox Code Playgroud)
但是,由于存在一些丢失的行,我需要在时间戳上加入数据而不是简单地压缩.我可以简单地忽略每个文件中没有匹配时间戳的任何行.
是否有一种pythonic方式在几行中实现此功能?
我的方法是依次推进每个迭代,直到它的时间戳不再小于迭代组的最大时间戳.每当所有时间戳匹配时,产生一行并推进所有迭代.但是,当我尝试实现这种方法时,逻辑看起来很混乱.
编辑:表现.
实现需要开始返回行而不首先将所有数据读入内存.读取所有数据需要一段时间,而且很多时候只需要检查第一批数据.
我想有效地将参数包中的参数与std :: array的元素相乘:
int index(auto... Is, std::array<int,sizeof...(Is)> strides)
{
// pseudo-code
// int idx = 0;
// for(int i = 0; i < sizeof...(Is); ++i)
// idx += Is[i] * strides[i];
// return idx;
}
Run Code Online (Sandbox Code Playgroud)
我无法完全围绕这一个.我开始沿着索引序列前进,但我可以弄清楚如何合并总和.
我使用的是c ++ 17,因此如果简化代码,折叠表达式就是公平的游戏.
谢谢你的任何指示.
编辑:澄清伪代码.唯一的伪部分是Is[i]引用第i个参数包参数的表达式.
TC的答案是完美的,这是我的最终代码,它是一个成员函数:
unsigned int index(auto... indexes)
{
unsigned int idx = 0, i = 0;
(..., (idx += indexes * m_strides[i++]));
return idx;
}
Run Code Online (Sandbox Code Playgroud)
在撰写本文时,代码使用带有-fconcepts标志的gcc 6.3.0进行编译,该标志引入了Concept TS.
使用auto... indexes是简写template<typename Args> f(Args... indexes).我尝试使用unsigned int概念作为参数,但我无法让它工作.
(...,)折叠是关键元素,并扩展为类似的东西(如果你实际上可以[]进入参数包中): …
****斯蒂芬的第1期解决方案 - 见下面的答案****
我\在语法表中标记为转义字符,但是然后覆盖Mathematica语法元素的指定\[Infinity].这是我的syntax-propertize-function:
(defconst math-syntax-propertize-function
(syntax-propertize-rules
("\\\\\\[\\([A-Z][A-Za-z]*\\)]" (0 "_"))))
Run Code Online (Sandbox Code Playgroud)
我从(defun math-node()函数中引用它是这样的:
(set (make-local-variable 'syntax-propertize-function)
math-syntax-propertize-function)
Run Code Online (Sandbox Code Playgroud)
在我的第一次尝试中,我没有使用该make-local-variable功能,当我的elisp缓冲区突出显示出错时,我感到很惊讶.
****第1期****的最终解决方案
我正在实现从cc模式派生的Emacs中的主模式,用于编辑Mathematica文件.目标是语法突出显示和缩进.我将在稍后与Mathematica内核进行接口连接.
我有基本的功能,但有一些困难点给我带来麻烦.
****问题1** - 该\字符用作转义字符,并为多字符,括号内的关键字添加前缀.**
像许多语言一样,Mathematica使用\角色逃脱",其他\角色是字符串.
数学一直是所谓的数学讲语法字符像\[Times],\[Element],\[Infinity]等等,代表运营商数学和常量.
而且,数学大量使用[和]替代(和)对函数定义和调用等
因此,如果我\在语法表中标记为转义字符,那么在使用语法字符的任何地方,我的括号都会出现错误匹配.例如,
If[x < \[Pi], True, False]
Run Code Online (Sandbox Code Playgroud)
当然,cc模式意图忽略之后的[权利\.鉴于Mathematica的功能特性,如果它不能与括号匹配,则模式几乎无用.认为lisp没有匹配.
如果我没有将\syntax-table作为转义字符放入,那么如何处理注释和字符串中的转义序列?
如果我可以将Times,Element,Infinity等放在关键字列表中并让一切正常工作,那就太好了.
****第2期** - Mathematica的语法与C,C++,Java,ObjC等不同,cc-mode的内置语法分析并不总能产生预期的结果.**
请考虑以下代码块:
FooBar[expression1, …Run Code Online (Sandbox Code Playgroud) 我想用于std::sort具有运行时记录长度的不透明数据类型。这是对类似问题的回答。
我尝试过间接对记录指针和索引进行排序,但这些方法的缓存局部性很差,因为比较需要访问数据集的随机分布的行。
我还尝试回退到允许运行时记录长度的C函数。qsort这样更好,但是需要一个回调函数来进行比较。这有两个缺点,1)与使用可内联的 lambda 相比,性能会受到影响;2)界面人体工学效果很差,因为您(可能)必须创建一个结构来保存函子以进行比较。
前面提到的答案建议,
带有专门交换的 pod 块伪引用迭代器,
但没有给出任何关于如何运作的提示。
总而言之,我想要一种方法来调用std::sort传递 lambda 来比较具有运行时记录长度的不透明数据类型数组,如以下代码所示,
#include <iostream>
#include <random>
using std::cout, std::endl;
int main(int argc, const char *argv[]) {
// We have 100 records each with 50 bytes.
int nrecords = 100, nbytes = 50;
std::vector<uint8_t> data(nrecords * nbytes);
// Generate random data for testing.
std::uniform_int_distribution<uint8_t> d;
std::mt19937_64 rng;
std::generate(data.begin(), data.end(), [&]() { return d(rng); });
int key_index = …Run Code Online (Sandbox Code Playgroud) 这个简单的示例代码为boost :: variant和boost :: apply_visitor:
#include <boost/variant/recursive_variant.hpp>
struct ExprFalse;
struct ExprTrue;
struct ExprMaybe;
typedef boost::variant<
ExprFalse,
ExprTrue,
ExprMaybe
> Expression;
struct ExprFalse { };
struct ExprTrue { };
struct ExprMaybe { };
struct Printer : public boost::static_visitor<>
{
public:
Printer(std::ostream& os) : m_os(os) { }
void operator()(ExprFalse const& expr) const { m_os << "False"; }
void operator()(ExprTrue const& expr) const { m_os << "True"; }
void operator()(ExprMaybe const& expr) const { m_os << "Maybe"; }
private:
std::ostream& m_os;
};
int …Run Code Online (Sandbox Code Playgroud) 我试图基于一个完整的类模板参数启用不同的成员函数,如下所示:
#include <type_traits>
template<int Dimension>
struct Foo
{
template<std::enable_if_t<Dimension == 1> = 0>
int bar(int i) const { return i; }
template<std::enable_if_t<Dimension == 2> = 0>
int bar(int i, int j) const { return i + j; }
};
int main(int argc, const char **argv)
{
Foo<1> a;
a.bar(1);
Foo<2> b;
b.bar(1,2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在c ++ - 14模式下使用gcc5,无法编译时出现以下错误:
tools/t1.cpp: In instantiation of 'struct Foo<1>':
tools/t1.cpp:18:12: required from here
tools/t1.cpp:13:9: error: no type named 'type' in 'struct std::enable_if<false, int>'
int …Run Code Online (Sandbox Code Playgroud) 我有以下增强python代码:
#include <boost/python.hpp>
namespace bp = boost::python;
class PyExtTest
{
public:
std::string get_name() { return m_name; }
void set_name(std::string const& name) { m_name = name; }
private:
std::string m_name;
};
BOOST_PYTHON_MODULE(test_ext)
{
bp::class_<PyExtTest>("pet")
.add_property("name", &PyExtTest::get_name, &PyExtTest::set_name)
;
}
Run Code Online (Sandbox Code Playgroud)
我使用distutils将其编译为以下目录层次结构:
build/lib/test_ext.so
Run Code Online (Sandbox Code Playgroud)
然后按以下方式使用(PYTHONPATH设置为build/lib):
import test_ext
a = test_ext.pet
a.name = 'dog'
print a.name # dog
Run Code Online (Sandbox Code Playgroud)
当我将distutils配置为test_ext作为package的一部分安装时pkg,python不再能够找到该模块。我最终得到目录层次结构:
build/lib/pkg
build/lib/pkg/__init__.py
build/lib/pkg/test_ext.so
Run Code Online (Sandbox Code Playgroud)
使用以下安装脚本:
test_module = Extension('pkg.test_ext'
, define_macros = module_macros
, extra_compile_args = module_compiler_flags
, sources = …Run Code Online (Sandbox Code Playgroud) 我试图将函数映射f到 tuplest0等t1以返回 tuple
std::tuple<f(std::get<0>(t0),std:get<0>(t1),...),f(std::get<1>(t0),std::get<1>(t1),...),...)。我有一个使用car,工作的版本cdr,cons但我正在尝试使用std::index_sequence.
代码:
// Helper
template<typename T>
using make_tuple_index = std::make_index_sequence<std::tuple_size<T>::value>;
// Implementation
template<typename F, typename... Ts, std::size_t... Is>
auto mapx_n_impl(const F& f, std::index_sequence<Is...>, const Ts&... t)
{ return std::make_tuple(f(std::get<Is>(t...))...); }
// Interface
template<typename T,
typename F,
typename Indices = make_tuple_index<T>>
auto map(const T& t, const F& f)
{ return mapx_impl(t, f, Indices{}); }
// Test
auto tup1 = std::make_tuple(1.0, 2.0, 3.0);
auto …Run Code Online (Sandbox Code Playgroud) MyConcept假设我有一个描述通用算法要求的概念algo。在下面的示例代码中,唯一的要求是一个自由函数my_free_function,它接受一个MyConcept类型并返回相同的类型,并且存在三种满足要求的类型。
template<class T>
concept MyConcept = requires (T a) {
{ my_free_function(a) } -> std::same_as<T>;
};
template<MyConcept T>
auto algo(T& a) {
return my_free_function(a);
}
namespace baz {
struct bar {};
bar my_free_function(bar) { return bar{}; }
}; // baz
unsigned long my_free_function(unsigned long) { return 42; }
namespace caz {
using NativeU64 = unsigned long;
NativeU64 my_free_function(NativeU64) { return NativeU64{42ul}; }
}; // caz
Run Code Online (Sandbox Code Playgroud)
我的挑战是,为了获得以下调用来编译 NativeU64 和 unsigned long,我必须在概念和算法之前定义 my_free_function 。这是唯一的选择吗?我当前的代码组织和依赖结构依赖于能够在头文件中定义概念和算法,编译器在看到满足要求的定义之前会使用这些概念和算法。
auto …Run Code Online (Sandbox Code Playgroud) c++ ×9
templates ×4
c++17 ×3
c++20 ×2
python ×2
python-3.x ×2
boost ×1
boost-python ×1
c++-concepts ×1
c++14 ×1
elisp ×1
emacs ×1
equality ×1
iterator ×1
lambda ×1
macos ×1
pickle ×1
polymorphism ×1
python-2.7 ×1
sfinae ×1
size ×1
sorting ×1
variant ×1