Skip to content

Error on non-existing arguments/parameters - #278

Merged
ddemidov merged 4 commits into
ev3dev:developfrom
pepijndevos:develop
Dec 24, 2016
Merged

ddemidov merged 4 commits into
ev3dev:developfrom
pepijndevos:develop

Conversation

@pepijndevos

Copy link
Copy Markdown
Contributor

This patch adds __slots__ to all API classes that use setattr magic and kwargs for function parameters. Besides reducing memory and speeding up access, the main point of this change is that a typo when calling a function will result in an error, rather than silently setting non-existing properties.

In [1]: import ev3dev.ev3 as ev3
In [2]: m = ev3.MediumMotor()
In [3]: m.run_to_abs_pos(position_sp=72*20, speed_sd=500, stop_action='coast')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-08bcba75fb47> in <module>()
----> 1 m.run_to_abs_pos(position_sp=72*20, speed_sd=500, stop_action='coast')

/home/robot/ev3dev-lang-python/ev3dev/core.py in run_to_abs_pos(self, **kwargs)
    799         """
    800         for key in kwargs:
--> 801             setattr(self, key, kwargs[key])
    802         self.command = self.COMMAND_RUN_TO_ABS_POS
    803 

AttributeError: 'MediumMotor' object has no attribute 'speed_sd'

Currently the last line would just set the speed_sd property on the object, without doing anything, leaving it up to you to figure out why your motor went berserk or did nothing at all.

With the proposed change, you get instant feedback that you're doing something stupid.

It turns out these are all auto generated, trashing previous manual
changes.
I hope I got all of them now.
@pepijndevos

Copy link
Copy Markdown
Contributor Author

What is _bin_data_size, what is it used for, and what is that piece of code trying to do? Obviously a class with __slots__ has no __dict__, but it seems adding it to __slots__ and checking for None would have the same effect? The property is not used anywhere else in the project, so maybe it could also just be a local variable?

@ddemidov

Copy link
Copy Markdown
Member

What is _bin_data_size, what is it used for, and what is that piece of code trying to do?

_bin_data_size stores the size of raw binary data to read from the sensor (see bin_data here).

In the code the field is used as a cache to store the value (and save an access to bin_data_format attribute). Initializing it with None should work as well.

@ensonic

ensonic commented Dec 21, 2016

Copy link
Copy Markdown
Contributor

If this makes property access faster it would be nice to see some timeit before and after. Could be a worthy addition to the release notes, since speed improvements help to cut latency on control loops.

@ddemidov ddemidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks good to me. Can you add the change for the _bin_data_size so that the tests pass?

@dwalton76, I think your mindcub3r example is the most extensive one we have. If you still have the robot around, could you please check this does not break anything?

We also have classes in ev3dev.helper that inherit from Motors. Do we need to add __slots__ there as well?

@dwalton76

Copy link
Copy Markdown
Collaborator

yep I will give it a try

@pepijndevos

Copy link
Copy Markdown
Contributor Author

I'm not sure what a good test is to test performance of this change, TBH probably most time is spend talking to the kernel, compared to one dictionary lookup.

I fixed the data size, which was rather straightforward.

The helper classes require a bit more thought. __slots__ don't mix well with multiple inheritance. The internet consensus seems to be that if you run into this you're using __slots__ wrong. It is suggested that they are a memory optimisation, not a means to "freeze" classes. The "Pythonic" way that is suggested is to overwrite __setattr__, which would make things slower, if anything, though the impact is probably manageable.

As it stands now, using the helper classes would add an internal dict, allowing undefined attributes as before.

@dwalton76

Copy link
Copy Markdown
Collaborator

I think this one needs some more testing. I cannot pinpoint what is wrong but when using this patch my robot failed to solve the rubiks cube three times in a row, without this patch it can solve it everytime. All motors look like they are rotating the correct amount (the cube would get jammed up otherwise) but I suspect one is turning in the wrong direction in some scenario.

@pepijndevos

pepijndevos commented Dec 23, 2016 via email

Copy link
Copy Markdown
Contributor Author

@ddemidov

Copy link
Copy Markdown
Member

Is this robot of yours one I can reproduce with my own ev3 (software and instructions available), or a custom monster with tons of extra parts?

This is not my robot, but I believe its built according to these instructions: http://mindcuber.com/mindcub3r/mindcub3r.html.

And the program is here: https://github.com/rhempel/ev3dev-lang-python/tree/develop/demo/MINDCUB3R

@dwalton76

Copy link
Copy Markdown
Collaborator

MINDCUBER is a good way to test, a lot of things have to be perfect for it to solve the cube. I'm actually using my own cube solver now, if you have a 42009 (the big crane) then you can build it. I need to put together instructions though.

Let me do this, I can put the cube back in the same scrambled state and record a video of it being solved with and without your patch. Then I can compare the two videos side by side and see where it is behaving differently. That should help narrow it down a bit.

@dwalton76

Copy link
Copy Markdown
Collaborator

Ignore what I said, I suspect I have an error on my end.

@ddemidov

Copy link
Copy Markdown
Member

A simple performance test shows there is a bit of improvement, or at least it is definitely not worse:

Before

>>> import timeit
>>> timeit.repeat('s.proximity', 'from ev3dev.ev3 import InfraredSensor; s = InfraredSensor()', number=100)
[4.144548124999972, 3.950399667000056, 4.128440291999993]
>>> timeit.repeat('s.proximity', 'from ev3dev.ev3 import InfraredSensor; s = InfraredSensor()', number=100)
[3.9438115419999917, 4.096679416999962, 3.8801820420000013]
>>> 

After

>>> import timeit
>>> timeit.repeat('s.proximity', 'from ev3dev.ev3 import InfraredSensor; s = InfraredSensor()', number=100)
[3.8830312090000234, 4.003896959000031, 3.78919083400001]
>>> timeit.repeat('s.proximity', 'from ev3dev.ev3 import InfraredSensor; s = InfraredSensor()', number=100)
[3.8567800419999685, 3.932827876000033, 3.8972169590000476]
>>> 

But I think the main idea here was to help users by catching semantic errors early.

@ddemidov
ddemidov merged commit 4a85fd2 into ev3dev:develop Dec 24, 2016
@ddemidov

Copy link
Copy Markdown
Member

Thanks!

@pepijndevos

Copy link
Copy Markdown
Contributor Author

Awesome. Still curious what happened on @dwalton76 's end though.

@dwalton76

Copy link
Copy Markdown
Collaborator

I have a bug where I sometimes assign the wrong color to a cube square based on the RGB values. In a nutshell you have 54 RGB tuples and from that you have to assign each square to be one of the six colors of the Rubik's cube. I had this fairly rock solid but have been working on this code to make it support 4x4x4, 5x5x5, etc cubes and must have introduced a bug because when I took a video of it solving with your patch in place I noticed that it got some of the color assignments wrong.

@dwalton76

Copy link
Copy Markdown
Collaborator

In another run I was able to solve a cube with your patch applied so the motor movements seem to be fine.

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.

4 participants