Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplifying version definitions #91

Open
illusional opened this issue Jan 20, 2021 · 0 comments
Open

Simplifying version definitions #91

illusional opened this issue Jan 20, 2021 · 0 comments

Comments

@illusional
Copy link
Member

Python can dynamically generate classes 😱

We could do some clever stuff to automatically generate these classes, for example:

from abc import ABC, abstractmethod

class BaseClass(ABC):
    def __init__(self, a=1):
        self.a = 1

    @abstractmethod
    def to_override(self):
        pass


class WithInheritedMethod:
    def to_override(self):
        print("Created a class called: " + self.__class__.__name__)


name = "CreatedClass"
globals()[name] = type(name, (WithInheritedMethod, BaseClass), {})

CreatedClass().to_override() # prints "Created a class called: CreatedClass"

We could apply a similar tactic to ToolVersions, for example to create a fully functional tool definition for Gatk4ApplyBqsr for each version of GATK, we could use:

versions = [Gatk_4_0_12, Gatk_4_1_2_0, Gatk_4_1_3_0, Gatk_4_1_4_0]


names = []
for v in versions: 
    # prepare the class name (NB: this is different to what we've used in the past)
    name = f"{Gatk4ApplyBqsrBase.tool(None)}_{v.__name__[5:]}"
    globals()[name] = type(name, (v, Gatk4ApplyBqsrBase, object), {})
    names.append(name)

# This overrides the exports and makes sure ONLY the created classes are exported
__all__ = names

Caveats

It does bring intellisense, pycharm / vscode can't determine that the import actually exists (red squiggly line):

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant