我有一个XML文件,foo.xml:
<foo>
<bar>
<baz phrase="hello"/>
</bar>
<quux phrase="goodbye"/>
</foo>
Run Code Online (Sandbox Code Playgroud)
我用这个Python代码解析它:
import lxml.etree as ET
# or if you don't have lxml: import xml.etree.ElementTree as ET
doc = ET.parse('foo.xml').getroot()
for elem in doc.findall('*[@phrase]'):
print(elem)
Run Code Online (Sandbox Code Playgroud)
这给了我:
<Element 'quux' at 0x7fa1419a1d18>
Run Code Online (Sandbox Code Playgroud)
现在我想找到具有phrase属性的所有元素,所以我尝试了'.//[@phrase]'然后findall()失败:
SyntaxError: invalid descendant
Run Code Online (Sandbox Code Playgroud)
我不明白什么是错的.如果我使用内置xml.etree.ElementTree而不是,则会出现相同的错误消息lxml.
请注意,虽然'.//'有效但返回但bar, baz, quux我不想要,bar因为它没有phrase属性.
我在Linux上使用XFS并且有一个内存映射文件,我每秒写一次.我注意到文件mtime(显示为watch ls --full-time)会定期更改但不规则.mtimes之间的差距似乎在2到20秒之间,但并不一致.在系统上运行的其他东西很少 - 特别是我的文件只有一个程序,还有一个读数.
同一个程序更频繁地写入其他一些mmapped文件,并且它们的mtime每30秒更改一次.
我没有使用msync()(在调用时会更新mtime).
我的问题:
我有一些代码使用 AVX2 内在_mm256_permutevar8x32_epi32又名vpermd通过索引向量从输入向量中选择整数。现在我需要同样的东西,但需要 4x32 而不是 8x32。 _mm_permutevar_ps是为浮点数做的,但我使用的是整数。
一个想法是_mm_shuffle_epi32,但我首先需要将我的 4x32 索引值转换为单个整数,即:
imm[1:0] := idx[31:0]
imm[3:2] := idx[63:32]
imm[5:4] := idx[95:64]
imm[7:6] := idx[127:96]
Run Code Online (Sandbox Code Playgroud)
我不确定什么是最好的方法,而且我不确定这是最好的方法。我正在寻找 Broadwell/Haswell 上最有效的方法来模拟 "missing" _mm_permutevar_epi32(__m128i a, __m128i idx)。如果可能的话,我宁愿使用 128 位指令而不是 256 位指令(即我不想扩大 128 位输入然后缩小结果)。
这可能是一个愚蠢的问题,但我盯着这段代码一段时间,却想不出什么是错的.在编译时:
#include <string>
#include <map>
template <typename T>
struct my_string_map {
typedef std::map<std::string, T> type;
};
template <typename T> bool add_to_string_map(my_string_map<T>::type map,
std::string str, T x) {
if (map.find(str) != map.end())
return false;
map[str] = x;
return true;
}
Run Code Online (Sandbox Code Playgroud)
我明白了:
foo.cpp:8: error: template declaration of ‘bool add_to_string_map’
foo.cpp:8: error: expected ‘)’ before ‘map’
foo.cpp:8: error: expected primary-expression before ‘str’
foo.cpp:8: error: expected primary-expression before ‘x’
Run Code Online (Sandbox Code Playgroud)
(my_string_map类的定义取自本论坛的另一个主题)
当我专注于我正在使用的模板类型时,例如
bool add_to_string_int_map(my_string_map<int>::type map,
std::string str, int x) {
if (map.find(str) != map.end())
return …Run Code Online (Sandbox Code Playgroud) 当我输入"show version"时,gdb显示以下内容:
GNU gdb (GDB) Red Hat Enterprise Linux (7.0.1-23.el5_5.2)
Run Code Online (Sandbox Code Playgroud)
我按照本网站上的说明使用Eclipse设置漂亮的打印:
http://wiki.eclipse.org/CDT/User/FAQ#How_can_I_inspect_the_contents_of_STL_containers.3F
当我尝试使用新的.gdbinit文件启动gdb时,出现以下错误:
Error in sourced command file:
Python scripting is not supported in this copy of GDB
Run Code Online (Sandbox Code Playgroud)
那么我就研究了一种实现这一目标的方法.我有7.0版本,所以它应该能够支持Python脚本.GDB的网站提到使用--with-python选项调用GDB配置脚本.但是,我在系统中的任何位置都看不到配置脚本.有多个开发人员使用这台机器,所以我不想重新安装GDB或类似的东西.有没有办法配置现有的GDB安装来启用python脚本?谢谢!
例如,我有一个元组
tuple<int, float, char> t(0, 1.1, 'c');
Run Code Online (Sandbox Code Playgroud)
和模板功能
template<class T> void foo(T e);
Run Code Online (Sandbox Code Playgroud)
并且我想用该函数循环元组元素,如何实现它,如使用boost :: mpl :: for_each实现以下内容?
template<class Tuple>
void loopFoo(Tuple t)
{
foo<std::tuple_element<0, Tuple>::type>(std::get<0>(t));
foo<std::tuple_element<1, Tuple>::type>(std::get<1>(t));
foo<std::tuple_element<2, Tuple>::type>(std::get<2>(t));
...
}
Run Code Online (Sandbox Code Playgroud) 我在脚本中有一个if语句,以查看通过argparse给出的目录是否是UNC路径.但不会以尾随斜线结束.
import re
#dest is set by argparse. but for this purpose i'll set it manually.
#originally i was escaping each \ in dest. But I found that argparse left them alone.
dest = '\\server-name\folder\subfolder'
if re.match ('^\\+[\w\W]+', dest ) and not re.match( '^\\+[\w\W]+\\$', dest):
dest = dest + '\\'
Run Code Online (Sandbox Code Playgroud)
我一直在用ipython玩这个.第一个声明无法匹配.我在Komodo IDE中使用了RxToolkit,它将正则表达式显示为匹配.我试过这个webtool:http://www.pythonregex.com/ 也匹配.有任何想法吗?
如果问题标题不够丰富,我很抱歉.请随意提出更好的变体.
我想执行以下任务:在一个目录中,我有许多文件是JPEG格式的照片.我想从EXIF中提取拍摄照片的日期,为每个日期创建一个新目录,并将文件移动到相关目录.
(EXIF日期和时间有格式YYYY:MM:DD hh:mm:ss,我希望将目录名格式化为YYYY-MM-DD,这就是我使用sed的原因)
我知道如何分别执行这些任务,但未能将它们组合在一起.我花了一些时间来调查如何执行命令,使用find带有-exec或xargs但仍然未能了解如何正确连锁一切.
最后,我能够使用两个命令完成我的任务:
find . -name '*.jpg' -exec sh -c "identify -format %[exif:DateTimeOriginal] {}
| sed 's/ [0-9:]*//; s/:/-/g' | xargs mkdir -p" \;
find . -name '*.jpg' -exec sh -c "identify -format %[exif:DateTimeOriginal] {}
| sed 's/ [0-9:]*//; s/:/-/g; s/$/\//' | xargs mv {}" \;
Run Code Online (Sandbox Code Playgroud)
但我不喜欢重复,我不喜欢-exec sh -c.有没有正确的方法在一行中执行此操作而不使用-exec sh -c?
这段代码编译干净,适用于我尝试过的所有编译器,除了GCC 8(和当前的GCC主干):
std::make_shared<volatile int>(0)
Run Code Online (Sandbox Code Playgroud)
我想知道:
std::atomic,但语义不一样所以建议使用它而volatile不是我正在寻找的.请在此处查看:https://godbolt.org/z/rKy3od
我希望此代码能正常工作,但无法编译:
#include <tuple>
struct S
{
int x = 0;
int y() const { return 1; }
};
bool f(const S& a, const S& b)
{
return std::tie(a.x, a.y()) < std::tie(b.x, b.y());
}
Run Code Online (Sandbox Code Playgroud)
GCC 9说:
错误:无法将类型为'int&'的非常量左值引用绑定到类型为'int'的右值
return std::tie(a.x, a.y()) < std::tie(b.x, b.y());
~~~^~
Run Code Online (Sandbox Code Playgroud)
代码有什么问题,如何解决,为什么?我正在尝试编写一个简洁的比较功能,通常会std::tie支持该功能(实际上这是的教科书用例std::tie)。