Python のような動的言語では、YAML マッピングをオブジェクトに簡単に変換できることがわかっています。これは非常に強力な機能であり、多くのコーディングを節約できます。
.yaml
ファイルをオブジェクトにマップしようとすると、問題が発生します。
ファイル: objtest.yaml
---
test: yaml test
option: this is option
...
私のコード:
class MyTest(object):
pass
testObj = MyTest()
f = open(os.path.join(os.path.dirname(__file__), 'objtest.yaml'))
rawData = yaml.safe_load(f)
print rawData
testObj.__dict__ = yaml.load(f)
f.close()
print testObj
STDOUT (トレースバックあり):
{'test': 'yaml test', 'option': 'this is option'}
Traceback (most recent call last):
File "C:/CROW/ATE/Workspace/Sandbox/test.py", line 23, in <module>
testObj.__dict__ = yaml.load(f)
TypeError: __dict__ must be set to a dictionary, not a 'NoneType'
質問:
ご覧のとおり、ファイルはロードされていますが、ファイルをロードしようとするとrawData
クラスインスタンスtestObj
に問題があり.yaml
ます。
私が間違っていることはありますか?