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

There are some similar code in Strategy and Filter #8

Open
jrycw opened this issue Dec 17, 2020 · 1 comment
Open

There are some similar code in Strategy and Filter #8

jrycw opened this issue Dec 17, 2020 · 1 comment
Labels

Comments

@jrycw
Copy link
Contributor

jrycw commented Dec 17, 2020

__init__, __call__ , set_parameters and show_parameters methods looks similar in Strategy and Filter.
My suggestion is using Inheritance to refractor the code as following:

class BaseDec:

    delattr_clsname = {'Strategy'}

    def __init__(self, **default_parameters):
        self.func = None
        self._variables = None
        self.filters = {}
        self._default_parameters = default_parameters
        self.set_parameters(default_parameters)

    def __call__(self, func):
        self.func = func
        return self

    def set_parameters(self, variables):
        if type(self).__name__ in self.delattr_clsname:
          stop_vars = ['sl_stop', 'tp_stop', 'ts_stop']
          for svar in stop_vars:
              if hasattr(self, svar):
                  delattr(self, svar)

        if variables:
            for key, val in variables.items():
                setattr(self, key, val)

        self._variables = variables

    def show_parameters(self):
        print(self._variables)

class Filter(BaseDec):
    ...

class Strategy(BaseDec):
    ...
@koreal6803
Copy link
Collaborator

Thanks for pointing out. I will try to merge it when I have extra time! Thank you!

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

No branches or pull requests

2 participants