forked from ev3dev/ev3dev-lang-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Anton Vanhoucke
authored and
Anton Vanhoucke
committed
Oct 7, 2015
1 parent
7da8f02
commit 345ea13
Showing
1 changed file
with
43 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here - it's a helpful function, but I'd prefer it in a separate module.
345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was just making functionality similar to the boostc python lbrary. That one allowed for motor.run_to_abs_pos(50). Why the different architecture?
345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The language bindings are generated automatically from the
autogen/spec.json
file in theev3dev-lang
repo. The spec covers the attributes in the file system that each device can have.Helper functions beyond these bindings belong in a separate module - I believe that @ddemidov wrote most of the boostc library by hand, so there may not have been the same design goals/constraints.
Besides, if the helper functions are in a separate module, they will be truly independent from the binding implementation :-)
345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rhempel, both C++ and python-boostc fully utilized autogen system. By the way, the spec does require the helper motor functions to be present, so may be they belong to the main module (or at least should be imported by default).
345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, now I see the section on helper functions - but we don't have a formal spec for what they are, do we? That means that each binding can implement whatever helpers they want.
I can certainly see the benefit of helpers, but I think they should be specified more formally. Can I ask you to open an issue on adding the helper functions to
spec.json
?345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The functions were discussed here: ev3dev/ev3dev-lang#89, we can still use the issue. The reason for rather informal specification is that we thought its difficult to specify them so that they would feel natural to each of the bindings languages.
345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @ddemidov, I'll bet we can come up with a spec for this - let's turn some ideas around in our heads...
345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@antonvh, have a look at README here: https://github.com/ev3dev/ev3dev-lang/tree/develop/autogen
345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that you can't spec them - all the functions return a certain type, just like the attributes, and they take parameters that we can specify, and we can add a default value that can be supported by certain languages. The implementation of the helper is usually a sequence of operations on the existing attributes. It should be specifiable - in theory.
In practice, we may use a language specific module to implement the helpers - I'm not saying we can't put helper functions into the
ev3dev.py
file, just that we should think about alternative implementations as well.345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The arguments are exactly the problem. In python a function can take
**kwargs
argument, and then you can writeIn C++ this is impossible. The closest thing I could come up with is
which is OK for C++, but is different.
345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, most people will stick with a preference. So for Python, can we think of a way that a user can do:
Where
ev3dev.py
has the helper functions, but it importsev3dev_binding.py
(or similar) where the raw bindings live? Something along those lines?@ddemidov, looking at the C++ and the python implementation, of run_timed() it may in fact be possible to make that a template...the text tokens are all quite similar.
345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could have a couple of submodules. For example,
ev3dev.core
would contain 'raw' bindings,ev3dev.extra
would contain syntactic sugar, and importingev3dev
would import both.Re C++ implementation with templates: I don't see how can this be solved with templates, can you elaborate? The function in python may have any number of parameters, and the names of the parameters are nowhere fixed. User can set any device attribute with this.
345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ddemidov, does the command chaining in C++ implementation need to be in a specific order?
345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just pushed a proof of concept here: [https://github.com/ev3dev/ev3dev-lang/tree/pure-python]
Just a basic helper function and a template that ignores parameters and defaults for now. I'll build it out a bit more later - needs a loop to evaluate parameters and build the function header, then an additional section for the attribute settings. Maybe it will not work at all or in general, but at least we tried ...
345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have played around with
autogen
ing helper functions and I'm not really happy with the results. My preference now is to have the helper functions written by hand in a separate module and take advantage of the Pythonic way of doing things, like this [http://stackoverflow.com/a/3394902] and this [http://stackoverflow.com/a/36908].Thanks to @antonvh and @ddemidov for making me learn more about Python and hopefully making me a better architect/programmer.
Besides, the
DcMotor
devices would need the some of the same helper functions likerun_timed()
and friends. I think there's a way to create an instance of aMotorHelper
class that takes one of the base EV3 motor classes in the__init__
and then figures out which methods apply - but it's too late tonight to figure it out.345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rhempel, re C++ command chaining:
It can support any order. It works like this: each
set_*
method returns a reference to*this
, so it can be immediately followed by any other method of the same class.345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rhempel, re
MotorHelper
idea:The spec.json provides a list of commands for each of the motors. This list is used by both C++ and python-boostc to generate the helper functions. Same approach could be used in pure-python.
345ea13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rhempel MotorHelper: the usual method to do this is build a superclass for the overlapping functions and subclasses for specific methods. This clashes with the structure of the generated code, though. So I'm afraid some repetition will occur.