Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Code cleanup #67

Merged
merged 12 commits into from Nov 10, 2016
Merged

Code cleanup #67

merged 12 commits into from Nov 10, 2016

Conversation

lechat
Copy link
Contributor

@lechat lechat commented Oct 30, 2016

Mostly cosmetic changes to code to look closer to idiomatic python:

  • removed unused imports
  • gave one character variables meaningful names
  • fixed all "missing space after" or "extra space before"

This is done for main.py only because the change is big enough even for a single file.

No logic have been changed and all works just fine on raspberry pi 3.

@lechat
Copy link
Contributor Author

lechat commented Oct 30, 2016

Those are the fixes for #21

@renekliment
Copy link
Member

Hey there. Thanks! It's good to see someone caring about the style, knowing it matters 😉

I've got quite a PR locally I'm gonna push that I'd like to merge first. Would you mind if we merged it first and you'd rebase / resolve merge conflicts to get your PR merged?

@lechat
Copy link
Contributor Author

lechat commented Oct 30, 2016

I don't mind at all.

Speaking about style, why not adopting PEP8? ;) I took me some time to switch from normal python vim settings to this project's style and I am sure that this style is a big turn off for seasoned python developers, who would like to contribute.

@renekliment
Copy link
Member

The current code is not PEP8 for historic reasons. As pointed out in the CONTRIBUTION.md, we'd like to go there. We've just been doing a lot of logic refactoring and I've wanted to do a cleanup (#21) that would rewrite the rest of the code to conform it afterwards. The reason is that a lot of code has been rewritten and there was no need to bother with code that was to be completely rewritten or removed.

The only thing where we'll go against PEP8 is tab indentation. If you agree with that, then cool. If you don't, I don't wanna start a tabs vs spaces flamewar 😄

@renekliment
Copy link
Member

Also, very cool with the pylintrc file! Right now, there's three very silly and basic commands on Travis CI because the length of the file got me scared when I was investigating it. We need to move to a file, so we can set settings easily and people can run it locally.

@lechat
Copy link
Contributor Author

lechat commented Oct 30, 2016

The pylintrc is quite freestyle now. As I am not faminiliar with code yet, it is quite relaxed. The only thing matters in this file is which checks are disabled and I disabled a few checks.

I also want to change all print based logging to standard python logging, so the code will not be peppered with "if debug" statements. Is that ok?

@renekliment
Copy link
Member

Could you post a link on the python logging thing you have in mind?

@renekliment
Copy link
Member

renekliment commented Nov 1, 2016

If you want to clean up the whole thing, please set up the checks also in .travis.yaml to run pylint.

@lechat
Copy link
Contributor Author

lechat commented Nov 1, 2016

Standard pythin logging: https://docs.python.org/2/library/logging.html

Just to get rid of "if debug: print()"

@renekliment
Copy link
Member

Yep, this is definitely the way to go. I see this as a new PR that comes after all the current PRs get merged. Feel free to open an issue for that so we keep track of it.

and 'command' in self.__pconfig['long_press']
and len(self.__pconfig['long_press']['command']) > 0
and 'duration' in self.__pconfig['long_press']):
if ('long_press' in self.__pconfig and
Copy link
Member

Choose a reason for hiding this comment

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

So I'm not sure I like this. My way, each additional condition is responsible for adding and and I can take out every single one of them without affecting other lines. The and-at-the-end-of-the-line way seems silly this way. This is in other places too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is default PEP8 style enforced by pylint: http://legacy.python.org/dev/peps/pep-0008/#should-a-line-break-before-or-after-a-binary-operator

But, as PEP8 says that it doesn't matter as long is the style is consistent across whole code, I will revert this.

Copy link
Member

Choose a reason for hiding this comment

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

Thank you! Please set up pylint to accept this.


time.sleep(.5) # time for the button input to settle down
while (GPIO.input(self.__pconfig['button']) == 0):
while GPIO.input(self.__pconfig['button'] == 0):
Copy link
Member

Choose a reason for hiding this comment

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

You've got a little error here with the parenthesis.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, removed wrong one...

@@ -10,13 +10,9 @@ before_install:

install:
- pip install -r src/requirements.txt
- pip install fakeRPiGPIO
- pip install -r src/dev-requirements.txt
Copy link
Member

Choose a reason for hiding this comment

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

Ah yes, this is a better idea!

- pylint --errors-only --extension-pkg-whitelist=alsaaudio,pyA20 src/alexapi
- pylint --errors-only src/auth_web.py
script:
- pylint --rcfile=pylintrc --extension-pkg-whitelist=alsaaudio,pyA20 src/*.py
Copy link
Member

Choose a reason for hiding this comment

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

If I'm correct:

  • we don't need --extension-pkg-whitelist=alsaaudio,pyA20 here anymore, since it's in the pylintrc file
  • this doesn't check the src/alexapi/ directory

@lechat
Copy link
Contributor Author

lechat commented Nov 5, 2016

There is a duplicated code in orangepi and raspberrypi. Perhaps that code needs to be moved into common ancestor for both devices.

@renekliment
Copy link
Member

Yes, I haven't thought it through entirely. I'll do a bit of cleanup on the device_platform front after we merge the open stuff.

@@ -3,7 +3,7 @@
import os
import threading

from pyA20.gpio import gpio as GPIO
from pyA20.gpio import gpio as GPIO # pylint: disable=import-error
Copy link
Member

Choose a reason for hiding this comment

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

What do you need this for? The pyA20 library should be installed on dev machines as well as on Travis CI.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed

Copy link
Member

Choose a reason for hiding this comment

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

Thanks!

@@ -64,7 +64,7 @@ def indicate_recording(self, state=True):
def indicate_playback(self, state=True):
GPIO.output(self.__pconfig['plb_light'], GPIO.HIGH if state else GPIO.LOW)

def detect_button(self, channel):
Copy link
Member

Choose a reason for hiding this comment

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

We actually need this. It's not used anywhere, but this is how the underlying C library calls it. It crashes if it's not there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then it is better to add that method with this signature to abstract class. And a comment about this in code, just in case somebody will wonder why it is there.

Copy link
Member

Choose a reason for hiding this comment

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

I'll do that in #73 then. Please put it back in the meantime.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will do it this evening

Copy link
Member

Choose a reason for hiding this comment

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

Great!

@@ -55,7 +55,7 @@ extension-pkg-whitelist=alsaaudio,pyA20
# E1103: %s %r has no %r member (but some types could not be inferred) - fails to infer real members of types, e.g. in Celery
# W0231: method from base class is not called - complains about not invoking empty __init__s in parents, which is annoying
# R0921: abstract class not referenced, when in fact referenced from another egg
disable=mixed-indentation,line-too-long,bad-continuation,missing-docstring,too-many-nested-blocks,too-many-branches,no-self-use,global-statement,too-many-statements
disable=mixed-indentation,line-too-long,bad-continuation,missing-docstring,too-many-nested-blocks,too-many-branches,no-self-use,global-statement,too-many-statements,duplicate-code,relative-import
Copy link
Member

Choose a reason for hiding this comment

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

Where do we have mixed indentation? That could (and should) be fixed easily I believe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mixed identation is "tabs for indentation and spaces for alignment". You just said that tabs are staying... ;)

Copy link
Member

Choose a reason for hiding this comment

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

Damn, stupid python. I totally forgot about python stuff when I wrote the guide. Let's please do tabs only then - could you also edit the contribution guide?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tabs only == rewriting indentation in all python files... I think that this is better to be done in separate pull request.

Copy link
Member

Choose a reason for hiding this comment

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

If I checked correctly, it's only a few places in main.py - the command line arguments and one line in orangepi.py. We can ignore tunein.py. It's an extenal thing that we need to pull new version of and stuff anyway.

@@ -15,4 +15,5 @@ install:
- cp src/config.template.yaml src/config.yaml

script:
- pylint --rcfile=pylintrc --extension-pkg-whitelist=alsaaudio,pyA20 src/*.py
- pylint --rcfile=pylintrc src/*.py src/alexapi/*.py
Copy link
Member

Choose a reason for hiding this comment

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

I believe we can squeeze these two lines into just pylint src/*.py src/alexapi

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, doesn't work. Pylint will only take one path...

Copy link
Member

Choose a reason for hiding this comment

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

It works for me. What does it give you?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It just silently skips second path for me. Add some unused import into raspbrerry_platform.py and run pylint with two paths - it will ignore this path.

Copy link
Member

Choose a reason for hiding this comment

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

https://gist.github.com/renekliment/6a8637deae834ea0d3fcb13e2a6c113e
It's not your branch, so it doesn't have the pylintrc file, but it should behave the same ... which it doesn't I guess.

@renekliment
Copy link
Member

renekliment commented Nov 5, 2016

Also, please add
-e git+https://github.com/xtacocorex/CHIP_IO.git#egg=CHIP_IO into dev-requirements.txt
and CHIP_IO into whitelisted modules in pylintrc.

We're close to merging this! I'll just make you a mess / nightmare by merging #68 and we're almost there 😃

Thank you for doing this - it's gonna be much better quality code after we're done!

@renekliment renekliment added this to the 1.3 milestone Nov 5, 2016
@renekliment
Copy link
Member

Please address my requests from my code review and I'm gonna test and merge this. We can then have a look at the logging stuff (yes, I've been spying in your repo 😄).

It's gonna take me some time to rework my PR so I'm gonna rebase on this later, when I've got that sorted out.

Thanks!

@renekliment
Copy link
Member

So I gave this a brief testing and everything seems to work fine (as before).

We could probably fix a few easy things due to which pylint3 fails, but there's gonna be many more things to make sure it works under python3 correctly, so we do that when doing #7.

Great job! Merging!

@renekliment renekliment merged commit 82a9718 into alexa-pi:master Nov 10, 2016
@renekliment
Copy link
Member

Oh, we still need to remove the spaces for indentation from CONTRIBUTING.md. I guess I'll just do that when updating docs when releasing v1.3.

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

Successfully merging this pull request may close these issues.

None yet

2 participants