Skip to content

Port type verification #425

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

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft

Port type verification #425

wants to merge 17 commits into from

Conversation

m-roberts
Copy link
Contributor

@m-roberts m-roberts commented Aug 10, 2021

Closes #338

@codecov
Copy link

codecov bot commented Aug 10, 2021

Codecov Report

Merging #425 (b87d1c3) into master (854e835) will decrease coverage by 4.29%.
The diff coverage is 5.88%.

❗ Current head b87d1c3 differs from pull request most recent head 56d182b. Consider uploading reports for the commit 56d182b to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master     #425      +/-   ##
==========================================
- Coverage   58.56%   54.26%   -4.30%     
==========================================
  Files          72       80       +8     
  Lines        2756     3282     +526     
==========================================
+ Hits         1614     1781     +167     
- Misses       1142     1501     +359     
Flag Coverage Δ
unittests 54.26% <5.88%> (-4.30%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
pitop/core/mixins/recreatable.py 45.45% <ø> (ø)
pitop/pma/button.py 42.10% <0.00%> (-11.23%) ⬇️
pitop/pma/buzzer.py 42.10% <0.00%> (-11.23%) ⬇️
pitop/pma/encoder_motor.py 72.22% <ø> (ø)
pitop/pma/led.py 42.10% <0.00%> (-11.23%) ⬇️
pitop/pma/adc_base.py 36.58% <20.00%> (-2.31%) ⬇️
pitop/robotics/simple_pid/PID.py
pitop/robotics/simple_pid/__init__.py
pitop/miniscreen/miniscreen.py 48.33% <0.00%> (ø)
pitop/miniscreen/oled/core/__init__.py 100.00% <0.00%> (ø)
... and 9 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a7614ea...56d182b. Read the comment docs.

@ghost ghost marked this pull request as ready for review August 11, 2021 23:28
@ghost
Copy link

ghost commented Aug 11, 2021

This PR address the related issue #338

When using PMA sensors, checks that the user has entered a valid port - both a sensible value like D0, and also that the port type is valid for the particular sensor being used.

Comment on lines 22 to 27
if port_name not in Port.keys():
raise ValueError(f"{port_name} is not a valid port name. An example of a valid port name is D0")

if not port_name.startswith("D"):
raise ValueError(f"{port_name} is not a valid port type for a button. Try using a digital port, such as D0")

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 think that this should be handled by a mixin (see Stateful, Recreatable...) so that it doesn't need to be duplicated. Not sure what I'd call it though...

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I was thinking the same, I'd just call it "AnalogComponent" or "DigitalComponent". I wouldn't necessarily call it a "mixin" though, it's just standard inheritance (Button is a DigitalComponent). Admittedly the name DigitalComponent does kind of suggest it does more than just port checking, so maybe "mixin" is the right terminology and it should be called DigitalPortValidator 😃

Copy link
Contributor Author

@m-roberts m-roberts Aug 12, 2021

Choose a reason for hiding this comment

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

Actually, Button inherits from gpiozero's Button class. It uses Stateful and Recreatable mixins (which are included, not inherited) - the same would be the case for this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Well yeah but it's still inheritance - the only difference between a mixin and inheritance is generally just if the parent can't be used standalone it's called a "mixin". But then that definition doesn't really apply to abstract base classes, which is still inherited and definitely can't be used standalone. Anyway this is just terminology at this point, I think the idea makes sense

@lgtm-com
Copy link

lgtm-com bot commented Aug 12, 2021

This pull request introduces 2 alerts when merging 43c9fcd into 4b4d30d - view on LGTM.com

new alerts:

  • 2 for Unused import

@m-roberts m-roberts marked this pull request as draft August 12, 2021 13:11
@lgtm-com
Copy link

lgtm-com bot commented Aug 12, 2021

This pull request introduces 2 alerts when merging 69fffe2 into 4b4d30d - view on LGTM.com

new alerts:

  • 2 for Unused import

@lgtm-com
Copy link

lgtm-com bot commented Aug 23, 2021

This pull request introduces 2 alerts when merging cdbdeb3 into 4b4d30d - view on LGTM.com

new alerts:

  • 2 for Unused import

@@ -1,7 +1,7 @@
from pitop import Button
from time import sleep

button = Button("D5")
button = Button("D8")
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 don't think this is relevant..?

def __init__(self, port_name):
self.port_name = port_name

if port_name not in valid_digital_ports:
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 think I would prefer to see regex used here to match ports: D[0-7]

Copy link
Contributor

Choose a reason for hiding this comment

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

good idea

@@ -22,6 +23,12 @@ class ADCBase(Stateful, Recreatable):
"""

def __init__(self, port_name, pin_number=1, name="adcbase"):
if port_name not in Port.keys():
raise ValueError(f"{port_name} is not a valid port name. An example of a valid port name is A0")
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's usually better to create a new Exception class specific for things like this, but we don't really have much in the way of uniform exception handling. Perhaps this should be created as a new Issue for across the SDK rather than tackling that here.

self._pma_port = port_name
self.name = name

if port_name not in Port.keys():
raise ValueError(f"{port_name} is not a valid port name. An example of a valid port name is D0 or A1")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same thing here re: regex and exceptions.

Wil Bennett and others added 3 commits September 2, 2021 17:13
Now uses regex instead of dictionary list, as per Mike's suggestion. Needs proper testing though.
@lgtm-com
Copy link

lgtm-com bot commented Sep 2, 2021

This pull request introduces 5 alerts when merging 32e3dc8 into dda2052 - view on LGTM.com

new alerts:

  • 3 for Missing call to `__init__` during object initialization
  • 2 for Unused import

@lgtm-com
Copy link

lgtm-com bot commented Sep 2, 2021

This pull request introduces 3 alerts when merging 5f1cd5b into dda2052 - view on LGTM.com

new alerts:

  • 3 for Missing call to `__init__` during object initialization

@ghost
Copy link

ghost commented Sep 2, 2021

@m-roberts I think this one's ready to be merged. I took into account your comments and used regex instead of dictionaries. Have tested it, and it's passed those automatic checks too. Do I just tag you here to request for it to be merged?

@lgtm-com
Copy link

lgtm-com bot commented Sep 3, 2021

This pull request introduces 3 alerts when merging 56d182b into a7614ea - view on LGTM.com

new alerts:

  • 3 for Missing call to `__init__` during object initialization

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

Successfully merging this pull request may close these issues.

PMA components should not allow a port of an invalid type
2 participants