想象一个使用动态向对象添加属性的函数setattr.这样做的原因是我想将一些外部结构(例如给定的参数树)映射到对象:
my_object = SomeClass()
apply_structure(my_object, some_descriptor)
my_object.device1.enabled = True
Run Code Online (Sandbox Code Playgroud)
从技术上讲这是有效的,但当然Pylint正确地抱怨'device1'不是其成员SomeClass.
我可以禁用警告,但这样会很糟糕(因为我仍然希望在因错误拼写等原因而不存在该属性的情况下获得警告).
是否有一种通用且合法(Pylint-proof)的方式来动态地将成员添加到不会导致警告的对象?
或者:我可以只为一个对象而不是行/块/文件禁用Pylint 吗?
说明:
您可能想知道为什么我计划在以后以硬编码方式访问这些属性时动态地为对象配备成员属性.
原因是:我有一个动态的程序部分(装饰发生的地方)和一个专门用于特定场景的静态部分.所以我也可以为这个场景创建一个静态类,但在很多情况下这样做太过分了.
以下专用代码可能允许访问可能连接到某个总线的设备的某些参数:
class MyDeviceHandler:
on_get_some_subtree_element(self):
return _some_internal_value
on_set_some_subtree_element(self, value):
_some_internal_value = value
dev = MyDeviceHandler()
decorate_object_with_device_structure(dev, 'some/attached/device')
dev.some.subtree.element = 5 <--- will call the set-callback
x = dev.some.subtree.element <--- will call the get-callback
Run Code Online (Sandbox Code Playgroud)
所以后面的结构'some/attached/device'可能是任意的,非常复杂,我不想在类结构中重现它.
摆脱此警告的一种方法是创建/访问dict基于树的方法:
dev['some']['subtree']['element'] = 5
Run Code Online (Sandbox Code Playgroud)
但是这写起来比较难写,而且阅读起来也不好 - 我只会这样做才能让Pylint安静下来.
我有与QT Creator中无法访问的本地和表达式中的变量所描述的相同的问题,但我想找到一种方法如何"修复"Qt Creator再次显示调试窗口中的std :: string等默认复合类型.
我通过dist升级(fedora 18 => 20)从Creator 2.8更新到3.0,我知道那里可能出现问题,但我真的想避免为Qt Creator重新安装整个操作系统.
有没有办法让Creator更详细或者写入日志文件?
知道Qt Creator 3.0是否能为复合类型显示出漂亮的值?
我可以检查哪些组件以及如何检查?
我正在寻找一个行为类似于boost::property_tree但(可选)将每个值项的get/set实现留给开发人员的数据结构.
你应该可以做这样的事情:
std::function<int(void)> f_foo = ...;
my_property_tree tree;
tree.register<int>("some.path.to.key", f_foo);
auto v1 = tree.get<int>("some.path.to.key"); // <-- calls f_foo
auto v2 = tree.get<int>("some.other.path"); // <-- some fallback or throws exception
Run Code Online (Sandbox Code Playgroud)
我想你可能会滥用property_tree这个但是我还没有考虑过实现,除非我知道这是一个预期的用例,否则我会对此感到不好.
编写一个处理请求的类,例如val = tree.get("some.path.to.key")通过调用提供的函数,首先看起来并不太难,但我可以想象很多特殊情况会使这个庞大的库变得庞大.
一些额外的功能可能是:
子树处理:不仅处理终端密钥,而且将某些子树转发给单独的实现.例如
tree.register("some.path.config", some_handler);
// calls some_handler.get<int>("network.hostname")
v = tree.get<int>("some.path.config.network.hostname");
Run Code Online (Sandbox Code Playgroud)在值/键之间搜索
boost::property_tree)是否有一个接近我正在寻找的图书馆?有没有人boost::property_tree为此目的使用经验?(通过继承或将特殊对象到树像例如描述这里)
我已经使用CMake和这个 CMake实用程序为Android成功配置并构建了一些Qt5应用程序.
一切正常,直到我从Qt5.6切换到Qt5.7.当我尝试配置时,我得到一个CMake错误,这对我没什么帮助:
-- Configuring done
CMake Error in CMakeLists.txt:
No known features for CXX compiler
"GNU"
version 4.9.
-- Generating done
-- Build files have been written to: /path/to/build-dir
Run Code Online (Sandbox Code Playgroud)
我像这样运行CMake:
ANDROID_SDK=/path/to/android-sdk-linux \
ANDROID_NDK=/path/to/android-ndk-r12 \
QT_ANDROID_ROOT=/path/to/Qt-5.7.0-android \
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk \
ANT=/usr/bin/ant \
cmake /path/to/CMakeLists.txt \
-DCMAKE_PREFIX_PATH=$QT_ANDROID_ROOT \
-DCMAKE_TOOLCHAIN_FILE=/path/to/android.toolchain.cmake
Run Code Online (Sandbox Code Playgroud)
我可以用最小的C++程序重现这种行为:
#include <iostream>
int main() { std::cout << "hi" << std::endl; }
Run Code Online (Sandbox Code Playgroud)
最小的CMakeLists.txt:
cmake_minimum_required(VERSION 3.1)
find_package(Qt5Core)
add_executable(foo main.cpp)
target_link_libraries(foo Qt5::Core)
Run Code Online (Sandbox Code Playgroud)
引入此错误的行是target_link_libraries(foo …
也许这只是缺少咖啡,但我正在尝试std::string从char已知最大长度的空终止数组创建一个我不知道的,如何做到这一点.
auto s = std::string(buffer, sizeof(buffer));
Run Code Online (Sandbox Code Playgroud)
..是我最喜欢的候选者,但由于C++字符串不是以空值终止的,因此该命令将复制sizeof(buffer)字节而不管任何包含的'\ 0'.
auto s = std::string(buffer);
Run Code Online (Sandbox Code Playgroud)
.. 发现buffer直到\0找到.这几乎是我想要的,但我不能相信接收缓冲区,所以我想提供一个最大长度.
当然,我现在可以strnlen()像这样集成:
auto s = std::string(buffer, strnlen(buffer, sizeof(buffer)));
Run Code Online (Sandbox Code Playgroud)
但是,这似乎是肮脏的-它遍历缓冲区两次,我必须处理C-工件,如string.h和strnlen()(和它的丑陋).
我如何在现代C++中做到这一点?
我正在为Android开发一个Qt5应用程序(使用CMake!),目前我正在尝试使用Qt读取位置数据QGeoPositionInfoSource.到目前为止,我的所有应用程序都做得很好,但是当我跑步时
auto source = QGeoPositionInfoSource::createDefaultSource(this);
Run Code Online (Sandbox Code Playgroud)
应用程序立即崩溃并logcat给我:
I/__log_qt( 422): (II) dpw_qt5: <last output from my app>
F/libc ( 422): Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 797 (QtThread)
I/DEBUG ( 333): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
I/DEBUG ( 333): Build fingerprint: 'samsung/trltexx/trlte:5.0.1/LRX22C/N910FXXU1BOE3:user/release-keys'
I/DEBUG ( 333): Revision: '12'
I/DEBUG ( 333): ABI: 'arm'
I/DEBUG ( 333): pid: 422, tid: 797, name: QtThread >>> …Run Code Online (Sandbox Code Playgroud) 颇为相似,这个问题我想包装与痛饮函数,该函数map的strings到stringS:
void foo(std::map<std::string, std::string> const& args);
Run Code Online (Sandbox Code Playgroud)
对于Python,它足以为地图创建别名:
namespace std {
%template(map_string_string) map<string, string>;
}
Run Code Online (Sandbox Code Playgroud)
代码生成器将创建包装函数map_string_string,甚至自动使用它.
my_module.foo({'a': 'b', 'c', 'd'})
Run Code Online (Sandbox Code Playgroud)
将被正确调用,不符合签名的值将被忽略.
我如何为JavaScript执行此操作?
我尝试了相同的(当然)和包装器生成但是当我尝试这样调用时foo:
my_module.foo({'a':'b', 'c':'d'});
Run Code Online (Sandbox Code Playgroud)
我明白了
/path/to/example.js:3
my_module.foo({'a':'b', 'c':'d'});
^
Error: in method 'foo', argument 1 of type 'std::map< std::string,std::string > const &'
at Object.<anonymous> (/path/to/example.js:8:7)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3 …Run Code Online (Sandbox Code Playgroud) 注意:由于这个答案不断获得好评 - 虽然仍然有 的用例TypedDict,但我今天会考虑使用 adataclass代替。
class BackupData(TypedDict, total=False):
archive_name: str
archive_size: int
transfer_size: int
transfer_time: float
error: str
def to_backup_data(data: Mapping[str, Any]) -> BackupData:
result = BackupData()
if 'archive_name' in data:
result['archive_name'] = str(data['archive_name'])
if 'archive_size' in data:
result['archive_size'] = int(data['archive_size'])
if 'transfer_size' in data:
result['transfer_size'] = int(data['transfer_size'])
if 'transfer_time' in data:
result['transfer_time'] = int(data['transfer_time'])
if 'error' in data:
result['error'] = str(data['error'])
return result
Run Code Online (Sandbox Code Playgroud)
即我有一个TypedDict带有可选键的并且想要一个 …
我用a LoggerAdapter来让我的python日志记录输出Linux TID而不是长的唯一ID.但是这种方式我不修改现有logger但我创建了一个新对象:
new_logger = logging.LoggerAdapter(
logger=logging.getLogger('mylogger'),
extra=my_tid_extractor())
Run Code Online (Sandbox Code Playgroud)
现在我希望LoggerAdapter某些模块使用它.只要我知道一个全局变量被用作记录器,我可以这样做:
somemodule.logger = new_logger
Run Code Online (Sandbox Code Playgroud)
但这并不好 - 它仅适用于几种情况,您需要知道模块使用的记录器变量.
你是否知道一种在LoggerAdapter全球范围内提供服务的方法,例如通过呼叫s.th. 喜欢
logging.setLogger('mylogger', new_logger)
Run Code Online (Sandbox Code Playgroud)
或者:还有其他一些方法可以让Python logging输出类似于打印的Linux线程ID ps吗?
I have two fairly identical (Linux-) systems but one with just a minimum set of packages installed. On one system I have a running (binary/ELF) executable which I want to copy over to the other system (with the minimum setup).
Now I need a way to copy all needed shared libraries as well. Currently I start the application on the source system and then go through the output of
lsof | grep <PID>
Run Code Online (Sandbox Code Playgroud)
or
ldd <FILE>
Run Code Online (Sandbox Code Playgroud)
to get a list …
c++ ×6
qt ×3
android ×2
android-ndk ×2
python ×2
arrays ×1
c++11 ×1
char ×1
cmake ×1
dependencies ×1
dynamic ×1
executable ×1
fedora ×1
javascript ×1
linux ×1
location ×1
logging ×1
module ×1
mypy ×1
pylint ×1
python-3.x ×1
qt-creator ×1
stdmap ×1
string ×1
swig ×1
types ×1