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

Remove repeated definitions of format_lines() #5215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nityeshaga
Copy link
Contributor

@nityeshaga nityeshaga commented Feb 27, 2018

Replaces the definitions of format_lines() in files- coala_main.py
and ShowAppliedPatchAction.py with import statements that import the
function from ConsoleInteraction.py.

Closes #5205

For short term contributors: we understand that getting your commits well
defined like we require is a hard task and takes some learning. If you
look to help without wanting to contribute long term there's no need
for you to learn this. Just drop us a message and we'll take care of brushing
up your stuff for merge!

Checklist

  • I read the commit guidelines and I've followed
    them.
  • I ran coala over my code locally. (All commits have to pass
    individually.
    It is not sufficient to have "fixup commits" on your PR,
    our bot will still report the issues for the previous commit.) You will
    likely receive a lot of bot comments and build failures if coala does not
    pass on every single commit!

After you submit your pull request, DO NOT click the 'Update Branch' button.
When asked for a rebase, consult coala.io/rebase
instead.

Please consider helping us by reviewing other peoples pull requests as well:

The more you review, the more your score will grow at coala.io and we will
review your PRs faster!

@@ -32,6 +26,7 @@ def apply(self,
FILE_DIFF_DICT_INDEX = 2
SECTION_INDEX = 3

from coalib.output.ConsoleInteraction import format_lines
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import statement had to be placed here instead of the usual - top of the file - because coala was encountering an error with circularly dependent import.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then put the function in a different module which doesnt cause a circular dependency

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a circular dependency problem with ShowAppliedPatches.py regardless of whether I put the function in ConsoleInteraction.py or coala_main.py. Now, the function format_lines() is used only in these 3 modules-- ShowAppliedPatches.py, ConsoleInteraction.py and coala_main.py.

Therefore, I IMHO I think that we need to put the function definition in one of these files only. Also, I don't think that it should be inside ShowAppliedPatches.py.

Hence, the confusion. 😕

Please suggest a fix to it @jayvdb

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is your task to find the solution.
I already know it.
And telling it to you doesn't help you learn.

Copy link
Member

@Naveenaidu Naveenaidu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nityeshaga LGTM 😄 . 👍 for mentioning the comment about the import statement.

@@ -32,6 +26,7 @@ def apply(self,
FILE_DIFF_DICT_INDEX = 2
SECTION_INDEX = 3

from coalib.output.ConsoleInteraction import format_lines
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then put the function in a different module which doesnt cause a circular dependency

@nityeshaga
Copy link
Contributor Author

I think that the Interactions.py module is the right home for the format_lines() function. ;-) So, I have moved its definition over there.

I have also moved the definitions of highlight_text() and color_letter() in that file too as IMHO they would kind of fall in the same category as format_lines().

@jayvdb What do you think?

@jayvdb
Copy link
Member

jayvdb commented Apr 10, 2018

get a green build. then we talk

@@ -2,7 +2,9 @@

from pyprint.NullPrinter import NullPrinter

from coalib.output.Interactions import fail_acquire_settings
from coala_utils.ContextManagers import retrieve_stdout
from coalib.output.Interactions import (fail_acquire_settings,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line contains following spacing inconsistencies:

  • Trailing whitespaces.

Origin: SpaceConsistencyBear, Section: python.

The issue can be fixed by applying the following patch:

--- a/tmp/tmp4c82t5xa/tests/output/InteractionsTest.py
+++ b/tmp/tmp4c82t5xa/tests/output/InteractionsTest.py
@@ -3,7 +3,7 @@
 from pyprint.NullPrinter import NullPrinter
 
 from coala_utils.ContextManagers import retrieve_stdout
-from coalib.output.Interactions import (fail_acquire_settings, 
+from coalib.output.Interactions import (fail_acquire_settings,
         highlight_text, color_letter, format_lines)
 from coalib.output.printers.LogPrinter import LogPrinter
 

@@ -2,7 +2,9 @@

from pyprint.NullPrinter import NullPrinter

from coalib.output.Interactions import fail_acquire_settings
from coala_utils.ContextManagers import retrieve_stdout
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file contains unused source code.

Origin: PyUnusedCodeBear, Section: flakes.

The issue can be fixed by applying the following patch:

--- a/tmp/tmp4c82t5xa/tests/output/InteractionsTest.py
+++ b/tmp/tmp4c82t5xa/tests/output/InteractionsTest.py
@@ -2,7 +2,6 @@
 
 from pyprint.NullPrinter import NullPrinter
 
-from coala_utils.ContextManagers import retrieve_stdout
 from coalib.output.Interactions import (fail_acquire_settings, 
         highlight_text, color_letter, format_lines)
 from coalib.output.printers.LogPrinter import LogPrinter

@@ -2,7 +2,9 @@

from pyprint.NullPrinter import NullPrinter

from coalib.output.Interactions import fail_acquire_settings
from coala_utils.ContextManagers import retrieve_stdout
from coalib.output.Interactions import (fail_acquire_settings,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code does not comply to PEP8.

Origin: PEP8Bear, Section: autopep8.

The issue can be fixed by applying the following patch:

--- a/tmp/tmp4c82t5xa/tests/output/InteractionsTest.py
+++ b/tmp/tmp4c82t5xa/tests/output/InteractionsTest.py
@@ -3,8 +3,8 @@
 from pyprint.NullPrinter import NullPrinter
 
 from coala_utils.ContextManagers import retrieve_stdout
-from coalib.output.Interactions import (fail_acquire_settings, 
-        highlight_text, color_letter, format_lines)
+from coalib.output.Interactions import (fail_acquire_settings,
+                                        highlight_text, color_letter, format_lines)
 from coalib.output.printers.LogPrinter import LogPrinter
 
 

def test_format_lines(self):
prompt_msg = 'Enter a number: '
prompt_msg_formatted = '[ ] Enter a number: '
self.assertEqual(format_lines(prompt_msg, symbol='['), prompt_msg_formatted)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code does not comply to PEP8.

Origin: PEP8Bear, Section: autopep8.

The issue can be fixed by applying the following patch:

--- a/tmp/tmp4c82t5xa/tests/output/InteractionsTest.py
+++ b/tmp/tmp4c82t5xa/tests/output/InteractionsTest.py
@@ -22,4 +22,5 @@
     def test_format_lines(self):
         prompt_msg = 'Enter a number: '
         prompt_msg_formatted = '[   ] Enter a number: '
-        self.assertEqual(format_lines(prompt_msg, symbol='['), prompt_msg_formatted)
+        self.assertEqual(format_lines(
+            prompt_msg, symbol='['), prompt_msg_formatted)

@@ -2,7 +2,9 @@

from pyprint.NullPrinter import NullPrinter

from coalib.output.Interactions import fail_acquire_settings
from coala_utils.ContextManagers import retrieve_stdout
from coalib.output.Interactions import (fail_acquire_settings,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W291 trailing whitespace

Origin: PycodestyleBear (W291), Section: autopep8.

from coalib.output.Interactions import fail_acquire_settings
from coala_utils.ContextManagers import retrieve_stdout
from coalib.output.Interactions import (fail_acquire_settings,
highlight_text, color_letter, format_lines)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E128 continuation line under-indented for visual indent

Origin: PycodestyleBear (E128), Section: autopep8.


color_letter(self.console_printer, message)

self.assertEqual(stdout.getvalue().strip(), first_part +
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line contains following spacing inconsistencies:

  • Trailing whitespaces.

Origin: SpaceConsistencyBear, Section: python.

The issue can be fixed by applying the following patch:

--- a/tmp/tmpz0nm9lb2/tests/output/InteractionsTest.py
+++ b/tmp/tmpz0nm9lb2/tests/output/InteractionsTest.py
@@ -44,7 +44,7 @@
 
             color_letter(self.console_printer, message)
 
-            self.assertEqual(stdout.getvalue().strip(), first_part + 
+            self.assertEqual(stdout.getvalue().strip(), first_part +
                                                         colored(letter, 
                                                                 color='blue') +
                                                         second_part)

color_letter(self.console_printer, message)

self.assertEqual(stdout.getvalue().strip(), first_part +
colored(letter,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line contains following spacing inconsistencies:

  • Trailing whitespaces.

Origin: SpaceConsistencyBear, Section: python.

The issue can be fixed by applying the following patch:

--- a/tmp/tmpz0nm9lb2/tests/output/InteractionsTest.py
+++ b/tmp/tmpz0nm9lb2/tests/output/InteractionsTest.py
@@ -45,7 +45,7 @@
             color_letter(self.console_printer, message)
 
             self.assertEqual(stdout.getvalue().strip(), first_part + 
-                                                        colored(letter, 
+                                                        colored(letter,
                                                                 color='blue') +
                                                         second_part)
 

text = 'Text to be highlighted'
#formatter = TerminalTrueColorFormatter(style=NoColorStyle)
#highlighted_text = highlight(text, TextLexer(), formatter)[:-1]
self.assertEqual(highlight_text(self.no_color, text, NoColorStyle),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line contains following spacing inconsistencies:

  • Trailing whitespaces.

Origin: SpaceConsistencyBear, Section: python.

The issue can be fixed by applying the following patch:

--- a/tmp/tmpz0nm9lb2/tests/output/InteractionsTest.py
+++ b/tmp/tmpz0nm9lb2/tests/output/InteractionsTest.py
@@ -53,5 +53,5 @@
         text = 'Text to be highlighted'
         #formatter = TerminalTrueColorFormatter(style=NoColorStyle)
         #highlighted_text = highlight(text, TextLexer(), formatter)[:-1]
-        self.assertEqual(highlight_text(self.no_color, text, NoColorStyle), 
+        self.assertEqual(highlight_text(self.no_color, text, NoColorStyle),
                             text) 

#formatter = TerminalTrueColorFormatter(style=NoColorStyle)
#highlighted_text = highlight(text, TextLexer(), formatter)[:-1]
self.assertEqual(highlight_text(self.no_color, text, NoColorStyle),
text)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line contains following spacing inconsistencies:

  • Trailing whitespaces.

Origin: SpaceConsistencyBear, Section: python.

The issue can be fixed by applying the following patch:

--- a/tmp/tmpz0nm9lb2/tests/output/InteractionsTest.py
+++ b/tmp/tmpz0nm9lb2/tests/output/InteractionsTest.py
@@ -54,4 +54,4 @@
         #formatter = TerminalTrueColorFormatter(style=NoColorStyle)
         #highlighted_text = highlight(text, TextLexer(), formatter)[:-1]
         self.assertEqual(highlight_text(self.no_color, text, NoColorStyle), 
-                            text) 
+                            text)

@@ -1,13 +1,25 @@
import unittest

from pyprint.NullPrinter import NullPrinter
from pyprint.ConsolePrinter import ConsolePrinter
from termcolor import colored
from pygments.formatters import TerminalTrueColorFormatter
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file contains unused source code.

Origin: PyUnusedCodeBear, Section: flakes.

The issue can be fixed by applying the following patch:

--- a/tmp/tmpz0nm9lb2/tests/output/InteractionsTest.py
+++ b/tmp/tmpz0nm9lb2/tests/output/InteractionsTest.py
@@ -3,7 +3,6 @@
 from pyprint.NullPrinter import NullPrinter
 from pyprint.ConsolePrinter import ConsolePrinter
 from termcolor import colored
-from pygments.formatters import TerminalTrueColorFormatter
 
 from coala_utils.ContextManagers import retrieve_stdout
 from coalib.output.Interactions import (fail_acquire_settings,

@jayvdb
Copy link
Member

jayvdb commented Apr 12, 2018

Please stop pushing until you have a version which passes the tests on your local machine.

@nityeshaga
Copy link
Contributor Author

Extremely sorry for the mess. 😅

Please stop pushing until you have a version which passes the tests on your local machine.

I'll keep this in mind

@@ -1,9 +1,18 @@
import unittest

from pyprint.NullPrinter import NullPrinter
from pyprint.ConsolePrinter import ConsolePrinter
from termcolor import colored
from pygments.formatters import TerminalTrueColorFormatter
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file contains unused source code.

Origin: PyUnusedCodeBear, Section: flakes.

The issue can be fixed by applying the following patch:

--- a/tmp/tmpx0fit6qr/tests/output/InteractionsTest.py
+++ b/tmp/tmpx0fit6qr/tests/output/InteractionsTest.py
@@ -3,7 +3,6 @@
 from pyprint.NullPrinter import NullPrinter
 from pyprint.ConsolePrinter import ConsolePrinter
 from termcolor import colored
-from pygments.formatters import TerminalTrueColorFormatter
 
 from coala_utils.ContextManagers import retrieve_stdout
 from coalib.output.Interactions import (fail_acquire_settings,

@nityeshaga nityeshaga force-pushed the removeRedun3 branch 3 times, most recently from baeca3e to 0d0a9fb Compare April 14, 2018 10:46
@@ -28,3 +45,35 @@ def fail_acquire_settings(log_printer, settings_names_dict):

logging.error(msg)
raise AssertionError(msg)


def highlight_text(no_color, text, style, lexer=TextLexer()):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so now instead of format_lines being duplicated, coalib/output/ConsoleInteraction.py:highlight_text is being duplicated here.

How is this better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I've fixed it now.
No duplicates of highlight_text() anymore :)

Copy link
Member

@jayvdb jayvdb Apr 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, why the Interactions module?

This module now needs a docstring that would explain why fail_acquire_settings and format_lines are grouped together.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better if I created a new module Formatting.py and moved format_lines(), highlight_text() and color_letter() over there?

Removes the different definitions of format_lines() in files-
ConsoleInteraction.py, coala_main.py and ShowAppliedPatchAction.py
and the definitions of highlight_text() and color_letter() from
ConsoleInteraction.py and moves them to Interactions.py.

Closes coala#5205
def sym(x): return ']' if x is '[' else x
return '\n'.join('{}{:>5}{} {}'.format(symbol, sym(symbol), line_nr, line)
for line in lines.rstrip('\n').split('\n'))
from coalib.output.Interactions import format_lines
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we even need to use format_lines in this module?

And are you sure that padding of 5 is not intentional, and 4 is good enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And are you sure that padding of 5 is not intentional, and 4 is good enough?

In ShowAppliedPatchesAction, format_lines() is used when printing the Severity of the patch.
Here's how it looked (before this PR):
Notice the line ! ![Severity: NORMAL]
showappliedpatchaction_on_master
And here's how it looks now (after this PR):
showappliedpatchesaction_on_removeredun3
So, the version of format_lines() in this PR works like it was expected to IMO.

why do we even need to use format_lines in this module?

Yes I agree, that was a bad decision on my part. 😅
I wasn't sure if creating a new file to solve a difficulty/low issue was allowed or not. Now I think it would be better if I create a new file (Formatting.py maybe?) and put format_lines(), color_letter() and highlight_text() inside it. What do you think @jayvdb ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No you missing my point. what does format_lines do? Why is that functionality needed in ShowAppliedPatchesAction.py?

Is it the right tool for the job?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does format_lines do?

format_lines returns a formatted version of the string passed to it as a parameter. It formats the string to make it look elegantly in coala's CLI by adding a little prefix to it.

Why is that functionality needed in ShowAppliedPatchesAction.py?

ShowAppliedPatchesAction already uses it to format the line which displays the Severity of any applied action.

So, unless you are suggesting that we change how ShowAppliedPatchesAction shows its messages, I think that format_lines is the right tool for the job.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still have not read format_lines and its usages carefully.
It is gobbledy-gook that people have copied and pasted without thinking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still have not read format_lines and its usages carefully.

😅 I'm sorry, I didn't find anything else to read about it except the 4 line definition which was present in all 3 of the above mentioned files.

So, are you suggesting that we modify the definition of format_lines()? maybe make pass the amount of padding as a parameter? Or are you suggesting that we remove the function altogether?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read those four lines very carefully. Understand exactly what happens when it is called each time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's what I understand format_lines() is doing:
Each time the function is called with a string (lines), it splits that string at \ns, prefixes each split string with the symbol (passed to it as a parameter), adds line_nr with a padding of 4, adds the sym() version of symbol, and finally joins them using \n and returns this joined version.
Also, sym() is a local function inside format_lines() which is just responsible to return the closing ] in case the symbol that is passed is [. Otherwise, it returns the passed character itself.

Isn't that all? 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I dont want you to tell me what it does in theory. I already know the answers.

You need to see why in practise , in some of the real world scenarios where it is used, it isnt particularly useful.

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

Successfully merging this pull request may close these issues.

Redefinition of the same function format_lines() in 3 files
4 participants