1

If you have a look at my lidtk repository, especially the classifiers, you can see that the following files are almost identical (current version in case this is fixed in future):

  • cld2_mod.py
  • langdetect_mod.py
  • langid_mod.py
  • text_cat.py

They all inherit from lidtk.LIDClassifier and they all have the commands

Usage: lidtk <<module name>> [OPTIONS] COMMAND [ARGS]...

  Use the <<module name>> language classifier.

Options:
  --help  Show this message and exit.

Commands:
  get_languages
  predict          
  print_languages
  wili             
  wili_k           
  wili_unk         

Is it possible to de-duplicate the click-code? I would like to use inheritance to de-duplicate the code.

1 Answer 1

1
+50

Glancing just shortly over your repo I think what you want is something like this:

import click

def group_factory(group, name, params):
    """This creates a subcommand 'name' under the existing click command
    group 'group' (which can be the main group), with sub-sub-commands
    'cmd1' and 'cmd2'. 'params' could be something to set the context
    for this group.
    """

    @group.group(name=name)
    def entry_point():
        pass

    @entry_point.command()
    @click.option("--foo")
    def cmd1(foo):
        print("CMD1", name, params, foo)

    @entry_point.command()
    @click.option("--bar")
    def cmd2(bar):
        print("CMD2", name, params, bar)

    return entry_point

You can either use the return value of group_factory as a main entry point in a set of different scripts:

if __name__ == "__main__":
    ep = group_factory(click, "", "something")
    ep()

... or you can use group_factory to repeatedly build the same sub-command hierarchy under some top-level command under different names (and with different params):

@click.group()
def cli():
    pass

group_factory(cli, "a", 1)
group_factory(cli, "b", 2)
group_factory(cli, "c", 3)
Sign up to request clarification or add additional context in comments.

1 Comment

This helped me to get rid of 400 lines of code: github.com/MartinThoma/lidtk/commit/… - thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.