Introspection¶
Sometimes there will be a need to discover what attributes and classes are defined in any imported json module.
Starting from vs 0.1.2 the importjson module does provide a comprehensive set of functions and methods for introspection of the contents of the python module generated by the imported json file.
Introspection objects¶
object fields ModuleAttributeInfo name - The name of the attributedefault - The default value of the attributeClassInfo name - The name of the Attributecls_ - The actual class object of this nameparent - The name of the parent class (or ‘object’)ClassAttributeInfo name - The name of the attributedefault - The default value of the attributeInstanceAttributeInfo name - The name of the attributedefault - The default value of the attribute
Module level Introspection¶
The imported module has two functions:
- <module>.get_attributes()
- A generator which will yield one or more
ModuleAttributeInfo
objects. The following code can be used to print the names of all module level fields :import importjson import jsonmodule for attribute in jsonmodule.get_attributes(): print( attribute.name )
- <module>.get_classes()
- A generator which will yield one or more
ClassInfo
objects. The following code can be used to print the names of all module level fields :import importjson import jsonmodule for class_info in jsonmodule.get_classes(): print( class_info_.name )
Class Level Introspection¶
Each class is provided with two introspection class methods :
- <class>.get_class_attributes()
- A generator which will yield one or more
ClassAttributeInfo
objects. The following code can be used to print the names of all class attributes of all classes in a module:import importjson import jsonmodule for class_info in jsonmodule.get_classes(): for attribute in class_info.cls_.get_class_attributes(): print(attribute.name)
- <class>.get_instance_attributes()
- A generator which will yield one or more
InstanceAttributeInfo
objects. The following code can be used to print the names of all instance attributes of all classes in a module:import importjson import jsonmodule for class_info in jsonmodule.get_classes(): for attribute in class_info.cls_.get_instance_attributes(): print(attribute.name)