2012-05-09 6 views
1

私は現在、有効な方法でこれを行う方法については空白です。オブジェクトを使用することを考えましたが、このケースでどのように役立つのか分かりません。何か案は?関数の辞書を結果の辞書に変換するのに、Pythonのトリックが必要

from random import choice 
from copy import deepcopy 


def main(): 

    def rand_int(): 
     return choice(['yes', 'no']) 

    # any nesting, functions possible 
    spec = { 
     'answer': rand_int, 
     'next': {'answer': rand_int}, 
     'the_answer': 42 
    } 

    #### looking for elegant (automatic) way to do this 
    result = deepcopy(spec) 
    result['answer'] = result['answer']() 
    result['next']['answer'] = result['next']['answer']() 
    #### until here 

    # result2 = ... 

    print 'result: %s' % result 


if __name__ == '__main__': 
    main() 

xsdを使用するように教えてください。

答えて

8

あなたはdictionary comprehension内の1つの行でこれを行うことができます。もちろん

{key: function() for key, function in mydictionary.items()} 

値が関数でない場合、これはエラーをスローしますので、それは可能性がある場合、我々は単に追加することができますthe callable() builtinに確認してください:

{key: (function() if callable(function) else function) for key, function in mydictionary.items()} 

私たちは、これは、それはもう少し複雑であるが、修正するにはあまりにも難しいことではないになり、あなたの答えは、再帰的にする必要があるという事実に対処する必要があります。

def call_all_callables_in_dict(mapping): 
    if hasattr(mapping, "items"): 
     return {key: call_all_callables_in_dict(value) for key, value in mapping.items()} 
    elif callable(mapping): 
     return mapping() 
    else: 
     return mapping 

items属性またはメソッドを持つオブジェクトをdictに保存する場合は、この機能が実行されるため、問題が発生する可能性があります。その属性またはメソッドの名前を変更するか、チェックをisinstance(dict)に置き換えることをお勧めします。

また、'yes'または'no'の文字列を返す誤った関数名rand_intについては、おそらくそれほど悪くないことにご注意ください。一般的には、これらの状況でもTrue/Falseが必要です。

Python 2.7より前のコメントに記載されているように、辞書の理解はできません。この問題を回避するには、dict()はタプルの発電機がかかりますので、あなたはそうのようなdictの理解を置き換えることができます。

{x: y for x, y in something.items()} 

で:だから

dict((x, y) for x, y in something.items()) 

を、フルに:

from random import choice 

def rand_int(): 
     return choice(['yes', 'no']) 

spec = { 
    'answer': rand_int, 
    'next': {'answer': rand_int}, 
    'the_answer': 42 
} 

def call_all_callables_in_dict(mapping): 
    if hasattr(mapping, "items"): 
     return {key: call_all_callables_in_dict(value) for key, value in mapping.items()} 
    elif callable(mapping): 
     return mapping() 
    else: 
     return mapping 

print(call_all_callables_in_dict(spec)) 

私たちに:

{'answer': 'no', 'the_answer': 42, 'next': {'answer': 'yes'}} 
+0

@TimPietzcker私が投稿していたときに、両方の回答を更新していることに気づきました。 –

+0

楽しみにしています:) –

+0

dictの補完はPython 2.7+で動作することに注意してください。また、構文はコロンで始まります: '{key:dict.items()のキーの値}' –

関連する問題