python - Good Pattern for Class-Based "Task" Modules? -
i'm attempting design easily-extendable/modular command-line application in python (2.7). each module called "task". there 1 task per file, contained in tasks/ directory, each containing subclass of task, , run() method containing code entry-point given task. want each "task file" contain needed register task system. each task contains metadata such help, available arguments, authentication information, categories, etc. these not static properties--for example, system uses templates replace $variables in text.
here's example code:
class testtask(task): description = "this task prints 'hello world...' , exits." = "usage: $argv0 $task ...\n ..." def __init__(self, arguments=none): super(testtask, self).__init__(description=self.description, arguments=arguments, help=self.help) def run(self): print("hello world...") so, then, how display task? or list of tasks descriptions? well, have scan directory, import, load, , instantiate each task call gethelp() or getdescription() on it. of these tasks heavy. loading time on "list tasks" on second , noticeable. thought of caching it, seems overkill.
what's cleaner/faster way this? should somehow cache task metadata? want dynamic possible ease development of tasks. thought inner class containing metadata, not sure. of task's imports still have processed believe, , i'm not clear on proper way share information between "meta" inner class , containing task. newer python wondering if i'm missing something. how have guys designed plugin models? thanks!
i'm not sure understand question, think claim
we have scan directory, import, load, , instantiate each task call gethelp()
is not true. example, class need not have instance instantiated (heavy) in order obtain static, compile-time data. loader quite fast, if working pre-parsed .pyc files.
i'm thinking example, worth noting
import task import testtask print(testtask.description) should instantaneous relative
tt = testtask() print(tt.get_help()) as no instance need created in former.
added example
i used python standard library of 194 modules containing 112k source lines import speed test. abridged code is:
import abc import aifc import anydbm import argparse import ast … import webbrowser import whichdb import xdrlib import xmllib import xmlrpclib import zipfile print(tty.setraw.__doc__) runs in 230ms on underpowered laptop. __doc__ attribute function docstring bound name @ import time, when loading pre-parsed .pyc files.
Comments
Post a Comment