You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As discussed in ev3dev/ev3dev#937, it would be helpful for many reasons to split each class or area into its own file.
The primary difficulty here is that we want to wait until all outstanding work is merged so that we don't accidentally lose anything. Naturally, git will treat all of these changes as deletions and creations rather than a clean delta.
and we are about to add evb.py and pistorms.py. Brainstorming....
Platforms
Maybe it is time for a platforms sub-dir to hold
brickpi.py
ev3.py
evb.py
pistorms.py
Moving ev3.py would make me a little nervous because so many people import it instead of auto.py. But if we were ever going to move it this would be the time.
Sensors
Have a sensors sub-dir? class Sensor could go in its init.py and then all of the other sensor classes could go in individual files. We could move all of the code that is in GyroBalancer.py to a gryo.py that holds class Gyro
Motors
Have a motors sub-dir? We could move the RemoteControlledTank class in helper.py to a motors/tank.py and get rid of helper.py (I don't think we need much of the other code in there at this point).
I'm going to have to claim ignorance here... I know almost nothing about the Python module/import system. Can you confirm/correct these beliefs? When importing a module...
If there's a directory by that name within the python path list its init.py is evaluated and the result imported
If a matching file is found, it is evaluated and the result imported
WRT your proposals, that sounds good. However, I have one question:
Imagine I have code which only uses motors and I want it to be cross-platform. If I import auto, as I understand it it'll have to load all of the individual components. Is that a significant perf hit? I presume if the pyc file is cached or we ship it pre-compiled, this won't be an issue, but I'm not sure of that.
Otherwise, I think the discussion about moving the platform files is interesting... at a minimum, we need to make sure we produce an "upgrading guide" with any and all changes explained with specific modifications one must make. I don't have an opinion on whether we should make that change yet.
There is one module for each major component, sensors, motors, lights, sound, display, etc.
There are lots of sensors, so there is a module for each vendor.
There is a platform module. It is private because now that there is /sys/class/board-info, all platform stuff can happen automagically. There is no more auto module.
Group all of the composite control classes/modules in to a control module (composite meaning it uses both sensors and motors to create some sort of control system)
Other points:
There can be fake platforms as well, for example a travis platform that sets up fake sysfs for testing (detected by the travis environment variable).
util module contains things like debug_print() that prints to stderr for VSCode extension compatibility
Module names are singular (this doesn't seem to be in PEP8, but I read it from some python guru somewhere recently and it sounded like a good idea at the time).
If there's a directory by that name within the python path list its __init__.py is evaluated and the result imported
True
If a matching file is found, it is evaluated and the result imported
True
Also, modules are only imported once, so if two files in the same program import the same module, the module is not re-evaluated.
If I import auto, as I understand it it'll have to load all of the individual components. Is that a significant perf hit?
If you turn it around so that each core module imports _platform (the new auto), then you get the efficiency of only importing the modules you are using.
For code varies by platform (take buttons for instance), will the user do from ev3dev import button and that in turn determines the platform and imports the appropriate platform/XYZ.py or will all button code for all platforms live in button.py? If the latter would we even need the platform dir? If its the former it might get a little confusing for the button code to be spread out in different directories.
For
|-- display.py
|-- led.py
How about
|-- display
| |-- led.py
| |-- screen.py
that way we are setup for anything that comes down the road (hdmi maybe).
😕 um... why are the LEDs under "display"? I view those as quite different things ("display" being the display) and have often used LEDs but almost never used the display.
Maybe visualoutput instead of display is a more descriptive (but way too long to actually use) way of describing how I group them together in my head :)
The other modules would use _platform to get things like paths and port names.
Maybe visualoutput instead of display is a more descriptive (but way too long to actually use) way of describing how I group them together in my head :)
HDMI on RPi already should work with the existing display class. It provides an fbdev, the same as all of the other displays that we use.
So, I don't really forsee any possible expansion in the visual output devices category. Better to keep it flat.
Nope. The _platform module figures out which platform and imports the appropriate submodule. e.g. ... The other modules would use _platform to get things like paths and port names.
How does the user access the OUTPUT_* and INPUT_* constants?
How does the user access the OUTPUT_* and INPUT_* constants?
First, I would suggest allowing the names 'A', 'B'... 1 (or '1'), 2... to be automatically converted to the correct port name. It's much shorter and pretty obvious. (If it quacks like a port name...)
Since the constants refer to ports, they should live in the port module. So the user might do this.
fromev3dev2.portimportOUTPUT_A
And the implementation in port.py might look like this...
For code varies by platform (take buttons for instance), will the user do from ev3dev import button and that in turn determines the platform and imports the appropriate platform/XYZ.py or will all button code for all platforms live in button.py? If the latter would we even need the platform dir? If its the former it might get a little confusing for the button code to be spread out in different directories.
I'm still trying to get my head wrapped around what will live where for this scenario.
As far as I can tell, the platform-specific part for buttons is just the path to the device node (/dev/input/...), so that is what would be be importing from _platform. The way I envisioned it is that the platform files are for the most part, just a bunch of names of things that are different between platform. The travis platform file might have more code for faking things for testing.
Activity
dwalton76 commentedon Sep 26, 2017
Once the Infrared PR is in I will not have any more EV3-G APIs in progress so I could pause on that front so we can split up core.py.
dwalton76 commentedon Sep 28, 2017
We should go ahead and figure out what we want the
ev3devdirectory structure to be. Today we haveand we are about to add evb.py and pistorms.py. Brainstorming....
Platforms
Maybe it is time for a
platformssub-dir to holdMoving ev3.py would make me a little nervous because so many people import it instead of auto.py. But if we were ever going to move it this would be the time.
Sensors
Have a
sensorssub-dir?class Sensorcould go in its init.py and then all of the other sensor classes could go in individual files. We could move all of the code that is in GyroBalancer.py to a gryo.py that holdsclass GyroMotors
Have a
motorssub-dir? We could move the RemoteControlledTank class in helper.py to a motors/tank.py and get rid of helper.py (I don't think we need much of the other code in there at this point).WasabiFan commentedon Sep 29, 2017
I'm going to have to claim ignorance here... I know almost nothing about the Python module/import system. Can you confirm/correct these beliefs? When importing a module...
init.pyis evaluated and the result importedWRT your proposals, that sounds good. However, I have one question:
Imagine I have code which only uses motors and I want it to be cross-platform. If I import
auto, as I understand it it'll have to load all of the individual components. Is that a significant perf hit? I presume if thepycfile is cached or we ship it pre-compiled, this won't be an issue, but I'm not sure of that.Otherwise, I think the discussion about moving the platform files is interesting... at a minimum, we need to make sure we produce an "upgrading guide" with any and all changes explained with specific modifications one must make. I don't have an opinion on whether we should make that change yet.
dlech commentedon Sep 29, 2017
Here's how I would do it (assuming breaking imports is not a concern)...
Key points:
automodule.controlmodule (composite meaning it uses both sensors and motors to create some sort of control system)Other points:
travisplatform that sets up fake sysfs for testing (detected by the travis environment variable).utilmodule contains things likedebug_print()that prints to stderr for VSCode extension compatibilityTrueTrueAlso, modules are only imported once, so if two files in the same program import the same module, the module is not re-evaluated.
If you turn it around so that each core module imports
_platform(the newauto), then you get the efficiency of only importing the modules you are using.WasabiFan commentedon Sep 29, 2017
I have to say, I really like @dlech's proposal (which is pretty much a superset of @dwalton76's). Some questions:
_platformand all the sub-modules?_platform, does it import all the other modules? This is whatautocurrently does.In my opinion, the answers should be 1) yes and 2) no, but that's just a preliminary thought.
dwalton76 commentedon Sep 29, 2017
For code varies by platform (take buttons for instance), will the user do
from ev3dev import buttonand that in turn determines the platform and imports the appropriateplatform/XYZ.pyor will all button code for all platforms live in button.py? If the latter would we even need theplatformdir? If its the former it might get a little confusing for the button code to be spread out in different directories.For
How about
that way we are setup for anything that comes down the road (hdmi maybe).
For
Would it be overkill to have each vendor have its own directory and then have a file per sensor?
WasabiFan commentedon Sep 29, 2017
😕 um... why are the LEDs under "display"? I view those as quite different things ("display" being the display) and have often used LEDs but almost never used the display.
dwalton76 commentedon Sep 29, 2017
Maybe
visualoutputinstead ofdisplayis a more descriptive (but way too long to actually use) way of describing how I group them together in my head :)dlech commentedon Sep 29, 2017
It could
Nope. The
_platformmodule figures out which platform and imports the appropriate submodule. e.g.The other modules would use
_platformto get things like paths and port names.HDMI on RPi already should work with the existing display class. It provides an
fbdev, the same as all of the other displays that we use.So, I don't really forsee any possible expansion in the visual output devices category. Better to keep it flat.
dlech commentedon Sep 29, 2017
Yes. Way overkill. 😃
WasabiFan commentedon Sep 29, 2017
How does the user access the
OUTPUT_*andINPUT_*constants?dlech commentedon Sep 29, 2017
First, I would suggest allowing the names
'A','B'...1(or'1'),2... to be automatically converted to the correct port name. It's much shorter and pretty obvious. (If it quacks like a port name...)Since the constants refer to ports, they should live in the
portmodule. So the user might do this.And the implementation in
port.pymight look like this...dwalton76 commentedon Oct 1, 2017
For code varies by platform (take buttons for instance), will the user do
from ev3dev import buttonand that in turn determines the platform and imports the appropriateplatform/XYZ.pyor will all button code for all platforms live inbutton.py? If the latter would we even need the platform dir? If its the former it might get a little confusing for the button code to be spread out in different directories.I'm still trying to get my head wrapped around what will live where for this scenario.
dlech commentedon Oct 1, 2017
As far as I can tell, the platform-specific part for buttons is just the path to the device node (
/dev/input/...), so that is what would be be importing from_platform. The way I envisioned it is that the platform files are for the most part, just a bunch of names of things that are different between platform. The travis platform file might have more code for faking things for testing.