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

Allow writing a simple decorated function instead of a bear #720

Closed
sils opened this issue Jul 2, 2015 · 8 comments
Closed

Allow writing a simple decorated function instead of a bear #720

sils opened this issue Jul 2, 2015 · 8 comments

Comments

@sils
Copy link
Member

sils commented Jul 2, 2015

most bears are just a function. Should be easy to have a decorator for this, right?

@sils
Copy link
Member Author

sils commented Jul 2, 2015

example

@local_bear
def KeywordBear(file, filename, keyword: str):
    for line in file:
        if keyword in line:
            yield Result("KeywordBear", filename, line, "Keyword found")

@sils
Copy link
Member Author

sils commented Jul 2, 2015

Prototype implementation: 9b75fc7

Not sure if that's the cleanest way to go but its the easiest by far.

@AbdealiLoKo
Copy link
Contributor

What's the benefit for this though? I think a standard/single way to make bears is simpler.

Instead of adding 1 line of decorator @local_bear, you'd have 1 line of class SpaceConsistencyBear(LocalBear):

Syntatically it's not become easier and multiple methods which are exactly equivalent (with no additional benefits) make it confusing.

@sils
Copy link
Member Author

sils commented Jul 2, 2015

@AbdealiJK the number of lines is actually the same in most cases.

One of the main aspects is that an analysis routine is by its nature a function. Throwing this into a class is unneeded and adds the possibility of hiding parameters for the analysis in the classes state. I.e. a function is more transparent and explicit. Also the look&feel is lighter and you get rid of four spaces extra indentation.

class SomeBear(LocalBear):
    def run(self, *args, **kwargs):
        self.section["hidden_secret"]  # Bears can steal settings behind users back
    @staticmethod
    def get_dependencies():
        return [Dependency]

vs.

@bear(BEAR_KIND.LOCAL, depends_on=[Dependency])
def do_something(*args, **kwargs):
    pass

So we have 4 spaces less indentation, three lines saved when using dependencies and the readability improved as well. It just makes sense to have the dependencies right above the function. It doesn't make sense to have a stupid "run" method, this name has absolutely no meaning.

@sils
Copy link
Member Author

sils commented Jul 2, 2015

plus with the decorator one could even build the function such that you could easily just execute the function without coala which is great for testing (i.e. the decorator would take care of getting dependency executed in that case)

@sils
Copy link
Member Author

sils commented Jul 2, 2015

So I'm in favor of full removal of the bear class and introduction of a decorator. It's only one decorator, not two! This allows us to fully get rid of the LocalBear/GlobalBear distinction in conjunction with #724 .

The bear decorator could just use a coala pool to parallelize over files and within coala there's no difference between global and local bears, there's just generic data parallel programming! This also implies that finally any bear can be easily be dependent on any other bear allowing to do file parallel processing after some global processing which is nice :)

@AbdealiLoKo
Copy link
Contributor

I'm in favour of it if youre removing the class :)

@Makman2 Makman2 added this to the 0.3 alpha milestone Jul 26, 2015
@sils
Copy link
Member Author

sils commented Aug 17, 2015

closing because growing chaotic, reopening new bug soonish.

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

No branches or pull requests

3 participants