YAML解析为对象(PyYAML Python3)

时间:2016-05-16 17:29:05

标签: python pyyaml

我已获得以下代码:

class Settings:
    def __init__(self, annual_volatility_target):
        self.annual_volatility_target = annual_volatility_target
        self.daily = annual_volatility_target/np.sqrt(252)

def yaml_load(name):
    with open('yaml/' + str(name) + '.yaml', 'r') as ymlfile:
        return yaml.load(ymlfile)

settings = yaml_load("settings")

使用以下YAML:

!!python/object:v.Settings
annual_volatility_target: 0.25

问题是,当我加载settings时,settings.daily未设置。 settings.annual_volatility_target__init__,无论我是否在Settings中这么说。

如果我手动实例化JsonObject obje = new JsonObject(); obje.addProperty("id","a"); obje.addProperty("NUMERO", 3); mClient.getTable("deneme").insert(obje); 对象(即不使用PyYAML),它可以正常工作。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

一种可能性是为Settings写一个constructor

import yaml
import numpy as np

class Settings:
    def __init__(self, annual_volatility_target):
        self.annual_volatility_target = annual_volatility_target
        self.daily = annual_volatility_target/np.sqrt(252)

def yaml_load(name):
    with open(str(name) + '.yaml', 'r') as ymlfile:
        return yaml.load(ymlfile)

def settings_constructor(loader, node):
    fields = loader.construct_mapping(node)
    return Settings(fields['annual_volatility_target'])

yaml.add_constructor('!v.Settings', settings_constructor)

settings = yaml_load("settings")

print(settings.annual_volatility_target)
print(settings.daily)

我必须使用修改后的yaml文件(我无法使其与注释!!python/object:v.Settings一起使用):

!v.Settings
annual_volatility_target: 0.25

答案 1 :(得分:0)

PyYAML中的Python对象分两步构建。首先调用__new__(在Constructor.make_python_instance()中),然后设置属性(在Constructor.set_python_instance_state()中)。这个两步过程是必需的,因为YAML支持对对象的引用,如果该对象是(间接)自引用的,它不能一次构造,因为它所依赖的参数(包括它自己)尚不可用。

您可以通过两种方式解决此问题。您可以为__setstate__()定义Settings,并使用dict调用__init__(),并从import yaml yaml_str = """\ !!python/object:try.Settings annual_volatility_target: 0.25 """ class Settings: def __init__(self, annual_volatility_target): self.__setstate__({annual_volatility_target: annual_volatility_target}) def __setstate__(self, kw): self.annual_volatility_target = kw.get('annual_volatility_target') self.daily = self.annual_volatility_target/np.sqrt(252) def __repr__(self): return "Setting({}, {})".format(self.annual_volatility_target, self.daily) settings = yaml.load(yaml_str) print(settings) 调用此内容:

daily

另一种更通用的(非PyYAML)解决方案是在首次访问时创建class Settings: def __init__(self, annual_volatility_target): self.annual_volatility_target = annual_volatility_target @property: def daily(self): return annual_volatility_target/np.sqrt(252) 值:

daily

如果您经常访问self._daily,那么您应该将其缓存在例如{0}中第一次计算值时class Settings: def __init__(self, annual_volatility_target): self.annual_volatility_target = annual_volatility_target self._daily = None @property: def daily(self): if self._daily is None: self._daily = annual_volatility_target/np.sqrt(252) return self._daily

<?php
header("content-type: image/png");
$qr_image = imagecreatefrompng("https://chart.googleapis.com/chart?chs=177x177&cht=qr&chl=MyHiddenCode&choe=UTF-8"); //no need for file_get_contents
$save = getcwd()."/temp/chart123.png";
imagepng($qr_image,$save); //save the file to $save path
imagepng($qr_image); //display the image