3

Super() を使用した多重継承のハンドルを持っていると思い、それを Facade クラス内で使用しようとしていましたが、奇妙なバグに遭遇しています。私は Fireworks と呼ばれるよくできた Python ワークフロー ソフトウェアを使用していますが、クラスとワークフロー タスクの構造はかなり厳密であるため、使いやすいように Facade クラスを作成していました。

以下は、ワークフロー タスクの基本構造です。

class BgwInputTask(FireTaskBase):

    required_params = ['structure', 'pseudo_dir', 'input_set_params', 'out_file']
    optional_params = ['kpoints', 'qshift', 'mat_type']

    def __init__(self, params): 
        self.structure = Structure.from_dict(params.get('structure').as_dict())
        self.pseudo_dir = params.get('pseudo_dir')
        self.kpoints = params.get('kpoints', None)
        self.qshift = params.get('qshift', None)
        self.isp = params.get('input_set_params')
        self.run_type = params.get('run_type', None)
        self.mat_type = params.get('mat_type', 'metal')
        self.filename = params.get('out_file')

        '''
        misc code for storing pseudo_potentials in:
        self.pseudo_files
        self.occupied_bands
        etc...
        '''

        params = {'structure': self.structure, 'pseudo_dir': self.pseudo_dir,
            'kpoints': self.kpoints, 'qshift': self.qshift,
            'input_set_params': self.isp, 'run_type': self.run_type,
            'mat_type':self.mat_type, 'out_file': self.filename}
        self.update(params)

    def write_input_file(self, filename):
        <code for setting up input file format and writing to filename>

super以下を使用して Facade クラスを構成しました。

class BgwInput(BgwInputTask):

    def __init__(self, structure, pseudo_dir, isp={},
                kpoints=None, qshift=None, mat_type='semiconductor',
                out_file=None):

        self.__dict__['isp'] = isp
        self.__dict__['run_type'] = out_file.split('.')[0]
        self.__dict__['params'] = {'structure': structure, 'pseudo_dir': pseudo_dir,
            'kpoints': kpoints, 'qshift': qshift,
            'input_set_params': self.isp, 'mat_type': mat_type,
            'out_file': out_file, 'run_type': self.run_type}
        print("__init__: isp: {}".format(self.isp))
        print("__init__: runtype: {}".format(self.run_type))

        super(BgwInput, self).__init__(self.params)

    def __setattr__(self, key, val):
        self.proc_key_val(key.strip(), val.strip()) if isinstance(
            val, six.string_types) else self.proc_key_val(key.strip(), val)

    def proc_key_val(self, key, val):
        <misc code for error checking of parameters being set>
        super(BgwInput, self).__dict__['params']['input_set_params'].update({key:val})

これは、私を混乱させる小さな警告を除いてうまく機能します。の新しいインスタンスを作成するときBgwInput、空のインスタンスは作成されません。以前のインスタンスで何らかの形で設定された入力セット パラメータは、新しいインスタンスに引き継がれますが、kpointsまたはではありませんqshift。例えば:

>>> epsilon_task = BgwInput(structure, pseudo_dir='/path/to/pseudos', kpoints=[5,5,5], qshift=[0, 0, 0.001], out_file='epsilon.inp')
__init__: isp: {}
__init__: runtype: epsilon

>>> epsilon_task.epsilon_cutoff = 11.0
>>> epsilon_task.number_bands = 29


>>> sigma_task = BgwInput(structure, pseudo_dir='/path/to/pseudos', kpoints=[5,5,5], out_file='sigma.inp')
__init__: isp: {'epsilon_cutoff': 11.0, 'number_bands': 29}
__init__: runtype: sigma

ただし、self.__dict__['isp'] = ispFacade クラスを変更すると、self.__dict__['isp'] = isp if isp else {}すべてが期待どおりに動作するようです。以前のインスタンスで設定されたパラメーターは、新しいインスタンスに引き継がれません。では、Facade クラスのデフォルトが isp={} に設定されていないのはなぜですか(これが __ init __ のデフォルトである場合)。デフォルトは空白の辞書であるべきなので、以前のパラメータはどこから取得していますか?

明確にするために、Facade クラスを期待どおりに機能させるための解決策があります ( ispFacade を に変更することによりself.__dict__['isp'] = isp if isp else {}) が、なぜこれが必要なのかを理解しようとしています。私はsuper、Python のメソッド解決順序に関する基本的な何かが欠けていると思います。なぜこれが起こっているのか、知識ベースを拡大しようとしていることに興味があります。

以下は、Facade クラスのメソッド解決順序です。

>>> BgwInput.__mro__

(pymatgen.io.bgw.inputs.BgwInput,
pymatgen.io.bgw.inputs.BgwInputTask,
fireworks.core.firework.FireTaskBase,
collections.defaultdict,
dict,
fireworks.utilities.fw_serializers.FWSerializable, 
object)
4

1 に答える 1