Skip to content
This repository was archived by the owner on Apr 20, 2022. It is now read-only.

remove need for re module #6

Merged
merged 1 commit into from
Jan 21, 2020
Merged

remove need for re module #6

merged 1 commit into from
Jan 21, 2020

Conversation

jepler
Copy link
Contributor

@jepler jepler commented Jan 21, 2020

The goal of the regular expression is to test that the letters in the byteorder string are only from the set R, G, B, W, and P. This strip() formulation achieves the same goal.

Copy link
Member

@ladyada ladyada left a comment

Choose a reason for hiding this comment

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

yes please!

@ladyada
Copy link
Member

ladyada commented Jan 21, 2020

good find!

@caternuson
Copy link
Contributor

This will help with #8

@ladyada
Copy link
Member

ladyada commented Jan 21, 2020

@caternuson is it fixed now?

@caternuson
Copy link
Contributor

@ladyada yep. seems like it. mpy-cross'd it and tried it out.
thanks @jepler

FAIL CASE

Adafruit CircuitPython 4.1.2 on 2019-12-18; Adafruit Gemma M0 with samd21e18
>>> import board
>>> import neopixel
>>> pixels = neopixel.NeoPixel(board.D1, 8, pixel_order="OMG_CUTE_KITTEH")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "neopixel.py", line 117, in __init__
  File "adafruit_pypixelbuf.py", line 55, in __init__
  File "adafruit_pypixelbuf.py", line 123, in parse_byteorder
ValueError: Invalid Byteorder string
>>> 

SUCCESS CASE

Adafruit CircuitPython 4.1.2 on 2019-12-18; Adafruit Gemma M0 with samd21e18
>>> import board
>>> import neopixel
>>> pixels = neopixel.NeoPixel(board.D1, 8, pixel_order="GRBW")
>>> pixels.fill(0xADAF00)
>>> pixels.brightness = 0.2
>>> pixels[2] = 0xff0000
>>> pixels[4] = 0x00ff00
>>> pixels[6] = 0x0000ff
>>> 

0121201235

Copy link
Contributor

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

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

This is clever! I hadn't seen strip used in this way.

@dhalbert dhalbert merged commit b7ef5e3 into adafruit:master Jan 21, 2020
@dhalbert
Copy link
Contributor

@caternuson did I merge this prematurely? I'm confused by the convo above.

@caternuson
Copy link
Contributor

@dhalbert nah, i don't think so. approved and tested. should be fine.

@tannewt
Copy link
Member

tannewt commented Jan 22, 2020

This doesn't validate internal characters because strip only trims the ends of a string. https://docs.python.org/3.8/library/stdtypes.html#str.strip

So, it will fail for "RXB".

You could use a set comparison instead.

@caternuson
Copy link
Contributor

Seems to work? Idea is to have nothing left after the strip. Otherwise, there's an unexpected character.

>>> "RGB".strip("RGBWP") != ""
False
>>> "GRB".strip("RGBWP") != ""
False
>>> "GRBW".strip("RGBWP") != ""
False
>>> "RGBW".strip("RGBWP") != ""
False
>>> "RXB".strip("RGBWP") != ""
True

@caternuson
Copy link
Contributor

But it's not robust the other way - if you spam it with acceptable characters:

>>> "RRGBGRGBRWWRGBRPPGBR".strip("RGBWP") != ""
False

@ladyada
Copy link
Member

ladyada commented Jan 22, 2020

cater, neither was the re version tho :)

@dhalbert
Copy link
Contributor

Doc: The outermost leading and trailing chars argument values are stripped from the string. Characters are removed from the leading end until reaching a string character that is not contained in the set of characters in chars. A similar action takes place on the trailing end.

So basically it's doing a set membership.

@tannewt
Copy link
Member

tannewt commented Jan 22, 2020

Ah, ok. I'm wrong. :-) Carry on.

adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Mar 7, 2020
Updating https://github.com/adafruit/Adafruit_CircuitPython_Pypixelbuf to 1.0.3 from 1.0.2:
  > build.yml: move pylint, black, and Sphinx installs to each repo; add description to 'actions-ci/install.sh'
  > Merge pull request adafruit/Adafruit_CircuitPython_Pypixelbuf#6 from jepler/no-re

Updating https://github.com/adafruit/Adafruit_CircuitPython_turtle to 2.0.0 from 1.1.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_turtle#18 from Marius-450/marius_improvements
  > build.yml: move pylint, black, and Sphinx installs to each repo; add description to 'actions-ci/install.sh'

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Added the following libraries: Adafruit_CircuitPython_Wiznet5k
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.

5 participants