develop.class_analyzer のソースコード

"""
クラスやオブジェクトのメンバーを解析・表示するスクリプト。

このスクリプトは、Pythonのオブジェクト(特にpymatgenのオブジェクト)の
関数、変数、属性を抽出し、その内容を整形して標準出力に表示します。
主にデバッグやオブジェクトの内部構造の調査に使用することを目的としています。

関連リンク: :doc:`class_analyzer_usage`
"""
from pymatgen.io.cif import CifParser
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer


[ドキュメント] def get_members(var): """ オブジェクトから callable (関数) なメンバーと非 callable (変数) なメンバーを抽出します。 この関数は、与えられたオブジェクトの型に基づいて、そのオブジェクトが持つメソッド(関数)と 通常の変数属性をリストアップします。また、オブジェクトの __dict__ 属性も取得します。 :param var: Any 解析対象のオブジェクト。 :returns: tuple (list, list, dict or None) callable なメンバーのリスト、非 callable なメンバーのリスト、オブジェクトの __dict__ 属性。 __dict__ が存在しない場合はNoneが返されます。 """ class_type = var.__class__ funcs = [method for method in dir(class_type) if callable(getattr(class_type, method))] vars = [method for method in dir(class_type) if not callable(getattr(class_type, method))] return funcs, vars, getattr(var, "__dict__", None)
if __name__ == "__main__": path = 'SrTiO3.cif' parser = CifParser(path) print_variables("parser", parser) mat = parser.get_structures(primitive = False, symmetrized = False)[0] #print_variables("mat", mat) sga = SpacegroupAnalyzer(mat) #print_variables("sga", sga) mat_sym = sga.get_symmetrized_structure() #print_variables("mat_sym", mat_sym) #lat_SrTiO3 = mat.lattice #print(lat_SrTiO3) eq_sites = mat_sym.equivalent_sites print_variables("eq_sites", eq_sites) asym_unit = [site[0] for site in eq_sites] print("mat_sym=", mat_sym) print("asym_unit=", asym_unit) print_variables("asym_unit[0]", asym_unit[0]) #print_variables("asym_unit[0]._abc_impl", asym_unit[0]._abc_impl) print_variables("asym_unit[0].specie", asym_unit[0].specie) print_variables("asym_unit[0].species", asym_unit[0].species)