![]() ![]() This method read the Python package or module and return the tuple of class objects.Ĭlass ValidatorA : def validate ( self ): print ( "validateA!!" ) base_path # OK Methods load_classes load_classes( set_setting ( singleton = True ) loader_c = ModuleLoader () loader_d = ModuleLoader () loader_e = ModuleLoader ( '/test' ) assert loader_c is loader_d # OK assert loader_c is loader_e # OK # The base_path is '/Users/user1/abc' assert loader_c. base_path ) # -> /Users/user1/abc # singleton setting ModuleLoader. base_path ) # -> /Users/user1/abc print ( loader_b. getcwd (), strict = True ) loader_a = ModuleLoader () loader_b = ModuleLoader () print ( loader_a. from autoload import ModuleLoader import os # global setting ModuleLoader. You can also create global setting and initialize singleton object. loader = ModuleLoader ( '/user/local/src/custom' )Ībout strict parameter, please see here. If you want to change the base path, you must generate the ModuleLoader with an absolute path parameter. In that case, the instance has the absolute path whereįrom autoload import ModuleLoader # The instance has '/usr/local/src/project/' loader = ModuleLoader () # load modules in the directory '/usr/local/src/project/validator/' validator_classes = loader. The ModuleLoader can be generated with no parameters. load_classes ( "validator" ) try : # initialize and execute method except : print ( "input is invalid!!" ) Install pip install autoload-module This library will give you comfortable Python metaprogramming.įrom autoload import ModuleLoader input = "foo bar baz" loader = ModuleLoader () # Automatically import modules and return class objects validator_classes = loader.
0 Comments
Leave a Reply. |