在 YAML 和 python 中 - 如何定义地图中的类型?
我很困惑。这就是我想要将我的文档构造为:
release: JULY
files:
# files
/foo/config.py: 1.6
/bar/dao.py: 1.19
然后我想根据 python 中给出的版本返回文件。这部分很简单,但它以浮点数形式返回值,这会带来问题。我想将它们强制转换为字符串而不进行预处理。 (我不想打开文件,循环遍历版本,并在每个条目前面添加 !!python/str )
我认为标签是执行此操作的方法,但我无法对文档进行头尾处理就我如何/在哪里定义标签而言。
有什么指点吗?
I'm stumped. here's what I want to structure my docs as:
release: JULY
files:
# files
/foo/config.py: 1.6
/bar/dao.py: 1.19
I then want to return files based on the release given in python. That part's easy, but it's returning the Values as floats, which poses a problem. I'd like to force them to strings without preprocessing. (I don't want to open a file, loop through the versions, and prepend !!python/str to each entry)
I assume that tags are the way to do this, but I can't make heads or tails of the documentation in terms of how/where I can define a tag.
Any pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要指定字符串的类型,如果您想将
float
值转储为字符串,只需先对它们调用str()
:如果您想要要序列化/反序列化自定义对象,您可以在某种程度上做到这一点,只需在类中定义
__repr__
即可:You shouldn't need to specify the type for a string, if you want to dump the
float
values as strings, just callstr()
on them first:If you want to serialize/deserialize custom objects, you can do that to some extent, just by defining
__repr__
in your class:看起来您想要做的是子类
Loader
并调用add_path_resolver
,如下所示:但我找不到有关
add_path_resolver
的任何文档以及SOME_TAG
和SOME_PATH
应该是什么。It looks like what you want to do is subclass
Loader
and calladd_path_resolver
, something like this:But I can't find any documentation on
add_path_resolver
and what theSOME_TAG
andSOME_PATH
should be.