小编mkl*_*mkl的帖子

`pydantic` 中的私有属性

我试图通过以下方式获得以下行为pydantic.BaseModel

class MyClass:
    def __init__(self, value: T) -> None:
        self._value = value

    # Maybe:
    @property
    def value(self) -> T:
        return self._value

    # Maybe:
    @value.setter
    def value(self, value: T) -> None:
        # ...
        self._value = value
Run Code Online (Sandbox Code Playgroud)

如果T也是一个 pydantic 模型,那么使用字典的递归初始化应该可以工作:

# Initialize `x._value` with `T(foo="bar", spam="ham")`:
x = MyClass(value={"foo": "bar", "spam": "ham"})
Run Code Online (Sandbox Code Playgroud)

请注意,它_value是使用 kwargs 初始化的value。验证也必须可用于私有字段。

pydantic 文档(PrivateAttr等)似乎暗示 pydantic 永远不会公开私有属性。我确信这有一些技巧。但是有没有一种惯用的方式来实现 pydantic 中的行为呢?或者我应该只使用自定义类?

python pydantic

11
推荐指数
1
解决办法
2万
查看次数

Catalina C ++:使用<cmath>标头会产生错误:全局命名空间中没有名为“ signbit”的成员

从Mojave升级到Catalina后,在环境中进行设置:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk。

我无法编译使用<cmath>标头的程序。

我尝试更改CFLAGS,CCFLAGS,CXXFLAGS以指向不变的MacOSSDK位置

Scanning dependencies of target OgreMain
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f OgreMain/CMakeFiles/OgreMain.dir/build.make OgreMain/CMakeFiles/OgreMain.dir/build
[  0%] Building CXX object OgreMain/CMakeFiles/OgreMain.dir/src/OgreASTCCodec.cpp.o
cd /Users/roman/Downloads/ogre-1.12.2/build/OgreMain && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -DOgreMain_EXPORTS -D__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES=0 -I/Users/roman/Downloads/ogre-1.12.2/OgreMain/src/OSX -I/Users/roman/Downloads/ogre-1.12.2/OgreMain/include/Threading -I/Users/roman/Downloads/ogre-1.12.2/OgreMain/src -I/Users/roman/Downloads/ogre-1.12.2/build/Dependencies/include -I/Users/roman/Downloads/ogre-1.12.2/OgreMain/include -I/Users/roman/Downloads/ogre-1.12.2/build/include -I/Users/roman/Downloads/ogre-1.12.2/OgreMain -isystem /usr/local/include  -Wall -Winit-self -Wcast-qual -Wwrite-strings -Wextra -Wundef -Wmissing-declarations -Wno-unused-parameter -Wshadow -Wno-missing-field-initializers -Wno-long-long -Wno-inconsistent-missing-override  -msse -O3 -DNDEBUG -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -fPIC -fvisibility=hidden -fvisibility-inlines-hidden   -std=c++11 -o CMakeFiles/OgreMain.dir/src/OgreASTCCodec.cpp.o -c /Users/roman/Downloads/ogre-1.12.2/OgreMain/src/OgreASTCCodec.cpp
In file included from /Users/roman/Downloads/ogre-1.12.2/OgreMain/src/OgreASTCCodec.cpp:29:
In file included from /Users/roman/Downloads/ogre-1.12.2/OgreMain/src/OgreStableHeaders.h:40:
In file included from /Users/roman/Downloads/ogre-1.12.2/OgreMain/include/OgrePrerequisites.h:309:
In file included …
Run Code Online (Sandbox Code Playgroud)

c++ macos clang toolchain macos-catalina

8
推荐指数
6
解决办法
294
查看次数

使用 AppleClang 时 decltype( std::devlcal&lt;std::ostream&gt;() &lt;&lt; std::declval&lt;T&gt;() ) 的奇怪结果

问题

考虑以下结构:

template<typename T>
struct stream
{
  using type = decltype(
      std::declval<std::ostream>() << std::declval<T>()
    );
};

template<typename T>
using stream_t = typename stream<T>::type;
Run Code Online (Sandbox Code Playgroud)

stream_t<T>正如我所料,当使用某些内置类型(int, float, ...)时, “值”Tstd::ostream&

但是,当使用std::stringcharint*或 的某些可流式虚拟结构时T,该类型是右值引用std::ostream&&

一旦std::declval<std::ostream>()(returns an std::ostream&&) 替换为std::declval<std::ostream&>(returns an std::ostream&,由于引用折叠规则,对吧?),返回的类型就是预期的std::ostream&。是否有一些operator<<我不知道的右值过载?

为什么会发生这种情况?

编译器规格

上述结果是使用AppleClang 11.0.0.11000033获得的。当使用 gcc-7.4 代替时,结果始终是std::ostream&,如预期的那样。

完整源码

#include <iostream>
#include <type_traits>

/* ************************************ …
Run Code Online (Sandbox Code Playgroud)

c++ decltype ostream c++17

6
推荐指数
1
解决办法
196
查看次数

使用“std”和“no_std”属性与包含 WASM 之间的关系

我刚刚开始研究基材,添加托盘教程已经让我严重困惑:

值得注意的是,Substrate 运行时会编译为本机 Rust std 二进制文件和 WebAssembly (Wasm) 二进制文件。std有关编译和功能的更多信息no_std,请参阅 XXX。

这很好,我很熟悉stdno_std。我的印象是,您要么已std启用并编译为本机 Rust 二进制文件,要么未启用并编译为 WASM 二进制文件。

但是,当我检查运行时时lib.rs,我发现以下内容:

#![cfg_attr(not(feature = "std"), no_std)]
// `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256.
#![recursion_limit = "256"]

// Make the WASM binary available.
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

// ...
Run Code Online (Sandbox Code Playgroud)

std这让我很困惑:为什么 WASM 二进制文件在启用时“可用” ?我本以为情况恰恰相反。在这种情况下,“可用”到底意味着什么?

我更深入地挖掘了一下,在旧版本的教程中发现了以下内容,该版本更加详细,导致了更严重的精神混乱:

这对于使 Substrate 运行时能够编译为本机二进制文件(支持 Rust std)和 Wasm …

rust blockchain substrate polkadot

5
推荐指数
1
解决办法
1066
查看次数