Skip to content

Split core.py into individual files #375

Closed

Description

@WasabiFan

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.

Activity

  1. dwalton76 commented on Sep 26, 2017

    @dwalton76
    Collaborator

    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.

  2. dwalton76 commented on Sep 28, 2017

    @dwalton76
    Collaborator

    We should go ahead and figure out what we want the ev3dev directory structure to be. Today we have

    dwalton@laptop ~/l/ev3dev-lang-python> ls -l ev3dev/
    total 212
    -rw-rw-r-- 1 dwalton dwalton   1033 Sep 22 15:21 auto.py
    -rw-rw-r-- 1 dwalton dwalton   2713 Sep 22 15:23 brickpi.py
    -rw-rw-r-- 1 dwalton dwalton 135150 Sep 25 10:32 core.py
    -rw-rw-r-- 1 dwalton dwalton   6118 Sep 22 15:23 ev3.py
    drwxrwxr-x 2 dwalton dwalton  12288 Sep 20 15:08 fonts/
    -rw-rw-r-- 1 dwalton dwalton  18404 Sep 22 15:23 GyroBalancer.py
    -rw-rw-r-- 1 dwalton dwalton   6637 Sep 25 10:28 helper.py
    -rw-rw-r-- 1 dwalton dwalton      0 Sep 22 15:21 __init__.py
    -rw-rw-r-- 1 dwalton dwalton  22396 Sep 22 15:23 webserver.py
    dwalton@laptop ~/l/ev3dev-lang-python> 
    

    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).

  3. WasabiFan commented on Sep 29, 2017

    @WasabiFan
    MemberAuthor

    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.

  4. dlech commented on Sep 29, 2017

    @dlech
    Member

    Here's how I would do it (assuming breaking imports is not a concern)...

    |-- __init__.py
    |-- _platform
    |   |-- __init__.py
    |   |-- brickpi.py
    |   |-- brickpi3.py
    |   |-- ev3.py
    |   |-- evb.py
    |   |-- pistorms.py
    |   `-- travis.py
    |-- button.py
    |-- control
    |   |-- __init__.py
    |   |-- gyro.py
    |   `-- rc_tank.py
    |-- display.py
    |-- led.py
    |-- motor.py
    |-- port.py
    |-- sensor
    |   |-- __init__.py
    |   |-- dexter.py
    |   |-- hitechnic.py
    |   |-- fatcatlab.py
    |   |-- lego.py
    |   `-- mindsensors.py
    |-- sound.py
    `-- util.py
    

    Key points:

    • 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.

  5. WasabiFan commented on Sep 29, 2017

    @WasabiFan
    MemberAuthor

    I have to say, I really like @dlech's proposal (which is pretty much a superset of @dwalton76's). Some questions:

    1. If I import the root module, does it import _platform and all the sub-modules?
    2. If I import _platform, does it import all the other modules? This is what auto currently does.

    In my opinion, the answers should be 1) yes and 2) no, but that's just a preliminary thought.

  6. dwalton76 commented on Sep 29, 2017

    @dwalton76
    Collaborator

    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).

    For

    |-- sensor
    |   |-- __init__.py
    |   |-- dexter.py
    |   |-- hitechnic.py
    |   |-- fatcatlab.py
    |   |-- lego.py
    |   `-- mindsensors.py
    

    Would it be overkill to have each vendor have its own directory and then have a file per sensor?

  7. WasabiFan commented on Sep 29, 2017

    @WasabiFan
    MemberAuthor
    |-- display
    |    |-- led.py
    

    😕 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.

  8. dwalton76 commented on Sep 29, 2017

    @dwalton76
    Collaborator

    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 :)

  9. dlech commented on Sep 29, 2017

    @dlech
    Member

    If I import the root module, does it import _platform and all the sub-modules?

    It could

    If I import _platform, does it import all the other modules? This is what auto currently does.
    

    Nope. The _platform module figures out which platform and imports the appropriate submodule. e.g.

    if platform == 'brickpi':
        from ev3dev2._platform.brickpi import *
    elif platform == 'ev3':
        from ev3dev2._platform.ev3 import *
    elif...

    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.

  10. dlech commented on Sep 29, 2017

    @dlech
    Member

    Would it be overkill to have each vendor have its own directory and then have a file per sensor?

    Yes. Way overkill. 😃

  11. WasabiFan commented on Sep 29, 2017

    @WasabiFan
    MemberAuthor

    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?

  12. dlech commented on Sep 29, 2017

    @dlech
    Member

    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.

    from ev3dev2.port import OUTPUT_A

    And the implementation in port.py might look like this...

    from ev3dev._platform import PORT_NAMES
    
    INPUT_1 = PORT_NAMES['1']
    ...
    OUTPUT_A = PORT_NAMES['A']
    ...
    class Port():
        def __init__(self, port):
            port = PORT_NAMES.get(str(port), port)
            ...
            
  13. dwalton76 commented on Oct 1, 2017

    @dwalton76
    Collaborator

    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.

  14. dlech commented on Oct 1, 2017

    @dlech
    Member

    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.

  15. self-assigned this
    on Oct 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions