<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>t+1 - Latest Comments in Help me rewrite some repetitive scripts</title><link>http://tplus1.disqus.com/</link><description></description><atom:link href="https://tplus1.disqus.com/help_me_rewrite_some_repetitive_scripts/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Mon, 19 Oct 2009 09:27:55 -0000</lastBuildDate><item><title>Re: Help me rewrite some repetitive scripts</title><link>http://blog.tplus1.com/blog/2009/10/18/help-me-rewrite-some-repetitive-scripts/#comment-20491587</link><description>&lt;p&gt;Linus, thanks for the idea!  I like the idea of using hooks that get specified for each case.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Wilson</dc:creator><pubDate>Mon, 19 Oct 2009 09:27:55 -0000</pubDate></item><item><title>Re: Help me rewrite some repetitive scripts</title><link>http://blog.tplus1.com/blog/2009/10/18/help-me-rewrite-some-repetitive-scripts/#comment-20388302</link><description>&lt;p&gt;One approach could be the "Template"-pattern (or "Self-Delegation", as it is sometimes called). Do a Base class "ScriptBase" like that:&lt;/p&gt;&lt;p&gt;----------&lt;br&gt;class ScriptBase(object):&lt;br&gt;    def __init__(self):&lt;br&gt;        self._p = optparse.OptionParser()&lt;/p&gt;&lt;p&gt;        self._p.add_option('--version', action='store_true',&lt;br&gt;             help='Print the version and exit')&lt;/p&gt;&lt;p&gt;    def set_parser_usage(self, usage):&lt;br&gt;        self._p.set_usage(usage)&lt;/p&gt;&lt;p&gt;    def pre_load_project_hook(self):&lt;br&gt;        pass&lt;/p&gt;&lt;p&gt;    def post_load_project_hook(self):&lt;br&gt;        pass&lt;/p&gt;&lt;p&gt;    def run():&lt;br&gt;        self._options, self._args = self._p.parse_args()&lt;br&gt;        if self.options.version:&lt;br&gt;            print_version()&lt;br&gt;            return&lt;br&gt;        self.pre_load_project_hook()&lt;/p&gt;&lt;p&gt;        pitzdir = Project.find_pitzdir(options.pitzdir)&lt;br&gt;        self._proj = Project.from_pitzdir(pitzdir)&lt;br&gt;        self._proj.find_me()&lt;/p&gt;&lt;p&gt;        self.post_load_project_hook()&lt;br&gt;        proj.save_entities_to_yaml_files()&lt;/p&gt;&lt;p&gt;# and a script inherits from it:&lt;/p&gt;&lt;p&gt;class PitzAttachFile(ScriptBase):&lt;br&gt;    def __init__(self):&lt;br&gt;        self.set_parser_usage("%prog entity file-to-attach")&lt;br&gt;    def pre_load_project_hook(self):&lt;br&gt;        if len(self._args) != 2:&lt;br&gt;        self._p.print_usage()&lt;br&gt;        return&lt;br&gt;    def post_load_project_hook(self):&lt;br&gt;        e, filepath = self._proj[args[0]], args[1]&lt;br&gt;        e.save_attachment(filepath)&lt;/p&gt;&lt;p&gt;# and here we run the script:&lt;br&gt;pitz_attach_file = PitzAttachFile()&lt;br&gt;&lt;a href="http://pitz_attach_file.run" rel="nofollow noopener" target="_blank" title="pitz_attach_file.run"&gt;pitz_attach_file.run&lt;/a&gt;()&lt;/p&gt;&lt;p&gt;-------&lt;/p&gt;&lt;p&gt;Note that this is totally untested and written in the comment field, so indentation might be wrong etc., but I hope to get across what this is all about - it's a "script framework" (Hollywood-principle: Don't call us, we call you).&lt;/p&gt;&lt;p&gt;You can make a hook mandatory by raising a NotImplementedError in the base class.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Linus</dc:creator><pubDate>Mon, 19 Oct 2009 03:52:00 -0000</pubDate></item></channel></rss>