Skip to content

Remove unneeded variables and conditions #63

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

Merged
merged 1 commit into from
Aug 7, 2019

Conversation

dcbriccetti
Copy link
Contributor

@dcbriccetti dcbriccetti commented Jul 31, 2019

I’ve recently discovered these helpful examples. I’d like to offer some code simplifications.

Accelerometer

If I’m not mistaken, x, y, and z are never (or extremely rarely) 0, so the “if”s are not needed, and then the R, G, B variables are not needed. Did I miss something?

Buttons

I find my new Python students are comfortable with conditional expressions, but if the
consensus here is that the if statement is preferred, I can remove that commit.

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

I do think my preference is to leave the if statements as is. The expression version is yet another form someone has to learn.

For the RGB case, is there any time x, y or z is something that can't be converted to an int? The if checks may be guarding against bad data.

@dcbriccetti dcbriccetti force-pushed the simplify-and-improve branch from d3fc1cb to 999ca3c Compare August 2, 2019 18:59
@dcbriccetti
Copy link
Contributor Author

dcbriccetti commented Aug 2, 2019

Hi Scott. Thanks for having a look.

I removed the if expression commit from the branch.

About the 9 lines of code (with three “if”s) to possibly ignore unreasonable values, how likely is that? Perhaps I’ll go follow the code and see what cpx.acceleration can return.

>>> type(_lis3dh.acceleration)
<class 'acceleration'>

I’m looking for this now. (This is a fascinating learning experience!)

I think it’s here. I see nothing indicating this returns anything other than a tuple of floats. Please educate me if I’m wrong.

@dcbriccetti
Copy link
Contributor Author

You know, I probably should have started by asking @kattni the reason for the “if”s.

@tannewt
Copy link
Member

tannewt commented Aug 6, 2019

I can't remember why they are there. Hopefully she does. If not, we can always try it and see if it breaks. :-)

@dcbriccetti
Copy link
Contributor Author

If the ifs are needed, an explanatory comment might be in order.

Copy link
Contributor

@kattni kattni left a comment

Choose a reason for hiding this comment

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

I think the idea was to be able to set a baseline value if you wanted for R, G and B.

@dcbriccetti
Copy link
Contributor Author

Thanks, @kattni.

What’s best to do here? Would you prefer to leave the file as it is?

@kattni
Copy link
Contributor

kattni commented Aug 7, 2019

Is it worth maintaining that functionality by retaining the R = 0 etc. and then changing this line to:

cpx.pixels.fill(((R + abs(int(x))), (G + abs(int(y))), (B + abs(int(z)))))

Turns into a lot of parens, but allows for increasing the overall brightness of things by allowing increased baseline RGB values.

Or does setting R = x + 0 and then making it cpx.pixels.fill((abs(int(R)), ... work? That would simplify the fill statement, and also might make it easier to see the RGB/xyz relationship.

I haven't tested either of those, I'm simply suggesting here.

@dcbriccetti What do you think?

@dcbriccetti
Copy link
Contributor Author

I think there’s value in explicitly showing the mapping between acceleration components and RGB components.

@dcbriccetti dcbriccetti force-pushed the simplify-and-improve branch from a9d5a97 to c27ddbf Compare August 7, 2019 19:49
Copy link
Contributor

@kattni kattni left a comment

Choose a reason for hiding this comment

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

I don't entirely understand what you've done here. If the intention was to simplify the code, I feel that we've moved away from the initial intention. You've introduced a significant number of new concepts. These examples, for the most part, are meant to be simple demonstrations of using the Circuit Playground Express library.

If you'd like to, I would suggest submitting your current iteration as an entirely new example. I don't want to entirely do away with the simple version which updating to this would do.

if z:
B = B + abs(int(z))
cpx.pixels.fill((R, G, B))
def color_amount(accel_component):
Copy link
Contributor

Choose a reason for hiding this comment

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

We typically include defs before the loop at the top of the code below the imports. Consider moving this.

@dcbriccetti
Copy link
Contributor Author

Where’s the embarrassed emoji? :-)

Yes, I sensed this was going the wrong direction.

@dcbriccetti dcbriccetti force-pushed the simplify-and-improve branch from c27ddbf to 999ca3c Compare August 7, 2019 19:54
@dcbriccetti dcbriccetti force-pushed the simplify-and-improve branch from 999ca3c to 1784060 Compare August 7, 2019 19:57
@dcbriccetti
Copy link
Contributor Author

retaining the R = 0 etc. ...

OK, I like that idea.

Copy link
Contributor

@kattni kattni left a comment

Choose a reason for hiding this comment

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

Looks great! Thanks for sticking with us through this!

@kattni kattni merged commit d675c99 into adafruit:master Aug 7, 2019
@dcbriccetti
Copy link
Contributor Author

dcbriccetti commented Aug 7, 2019

Glad to be working with you to help with education.

Do you think there’s much value to the Adafruit audience of my more complicated version? I can always just use it with my students, and maybe make a YouTube video explaining it.

@dcbriccetti dcbriccetti deleted the simplify-and-improve branch August 7, 2019 20:08
@kattni
Copy link
Contributor

kattni commented Aug 7, 2019

@dcbriccetti I think there's value to having multiple options. I would suggest explaining it thoroughly though, as it was unclear to me, so I imagine it will be unclear to many. If you make a video explaining it, you could include a link in a comment.

@dcbriccetti
Copy link
Contributor Author

The video is here, https://youtu.be/eNpPLbYx-iA, and I’ll make a new pull request for the new program.

adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Aug 8, 2019
Updating https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground to 2.1.1 from 2.1.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_CircuitPlayground#63 from dcbriccetti/simplify-and-improve
  > Merge pull request adafruit/Adafruit_CircuitPython_CircuitPlayground#64 from hexthat/patch-4
  > Merge pull request adafruit/Adafruit_CircuitPython_CircuitPlayground#61 from brettcannon/patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_Crickit to 2.1.6 from 2.1.5:
  > Merge pull request adafruit/Adafruit_CircuitPython_Crickit#20 from caternuson/iss19
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.

3 participants