Skip to content

pybricksdev: add import inlining option - #20

Closed
thesynman wants to merge 14 commits into
pybricks:masterfrom
thesynman:master
Closed

thesynman wants to merge 14 commits into
pybricks:masterfrom
thesynman:master

Conversation

@thesynman

Copy link
Copy Markdown

No description provided.

@dlech dlech 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.

Wow! This is very ambitious. We do occasionally get questions about how to share code, so I think this is a useful addition. Really nice to have unit tests too.

I've made a bunch of inline comments and suggestions. Also we just started using black to automatically format the code for us, so if you could poetry run black ., that will take care of formatting.

Comment thread .gitignore Resolved
Comment thread README_inline.md Resolved
Comment thread README_inline.md Resolved
Comment thread README_inline.md Resolved
Comment thread pybricksdev/cli/__init__.py Resolved
Comment thread pybricksdev/inline.py Resolved
Comment thread pybricksdev/inline.py Resolved
Comment thread tests/test_inline.py Resolved
Comment thread .gitignore Resolved
@thesynman

Copy link
Copy Markdown
Author

@dlech Thanks for taking time to review this. I'll make the requested changes as soon as I get some time.

@thesynman

Copy link
Copy Markdown
Author

@dlech I think I've addressed all your comments now. Please can you check and review. Happy to make more changes if needed. Thanks.

@dlech dlech 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.

Thanks for making the changes. I found a few more things to look at. Also, it looks like you may have used an older version of black to format the code. We use poetry to manage packages and ensure everyone is using the same versions of the development dependencies (see pyproject.toml and poetry.lock)

Comment thread .gitignore

# Temporary files
demo/_*.py
/tests/inline_test_resources/**/*.flat.py

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.

It would be better to create a new .gitignore file in the inline_test_resources directory and just add *.flat.py there. That way if directories ever get moved or renamed, things won't break.

Comment thread README_inline.md
@@ -0,0 +1,118 @@
# Inlining imports with pybricksdev
Most PoweredUp hubs are too small to have file systems, so splitting your PyBricks code

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.

Suggested change
Most PoweredUp hubs are too small to have file systems, so splitting your PyBricks code
Most Powered Up hubs are too small to have file systems, so splitting your Pybricks code

Comment thread README_inline.md
that refer to modules in the host file system should be flattened into the
main script, so that the resulting program is still in one file. The intention is to
retain compatibility with desktop Python so that PyBricks code could, with simulated
PyBricks libraries, be run on the desktop.

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.

Suggested change
PyBricks libraries, be run on the desktop.
Pybricks libraries, be run on the desktop.

Comment thread README_inline.md
for the 'run' and 'compile' functions in pybricksdev specifies that imports
that refer to modules in the host file system should be flattened into the
main script, so that the resulting program is still in one file. The intention is to
retain compatibility with desktop Python so that PyBricks code could, with simulated

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.

Suggested change
retain compatibility with desktop Python so that PyBricks code could, with simulated
retain compatibility with desktop Python so that Pybricks code could, with simulated

Comment thread README_inline.md
### The --inline-imports option
Here's an example command line:

pybricksdev run --inline-imports True ble "Pybricks Hub" demo/shortdemo.py

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.

Suggested change
pybricksdev run --inline-imports True ble "Pybricks Hub" demo/shortdemo.py
pybricksdev run --inline-imports ble demo/shortdemo.py

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.

Also, demo/shortdemo.py doesn't import anything and people won't have it anyway if they install pybricksdev from PyPI. So the generic main.py might be a better example file name.

Comment thread pybricksdev/inline.py
Comment on lines +14 to +16
script_path: Path to the script to be processed.
import_base: Path to a secondary root directory for inlined imports. Optional.
The return value is the path to the flattened script

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.

Suggested change
script_path: Path to the script to be processed.
import_base: Path to a secondary root directory for inlined imports. Optional.
The return value is the path to the flattened script
Args:
script_path: Path to the script to be processed.
import_base: Path to a secondary root directory for inlined imports. Optional.
Returns:
The path to the flattened script.

parser.add_argument(
"--import-path",
dest="importbase",
help="Additional base dir for inlined imports. Ignored unless --inline-imports is True",

@dlech dlech May 25, 2021 •

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.

All of the other help message already start with lower case.

Suggested change
help="Additional base dir for inlined imports. Ignored unless --inline-imports is True",
help="additional base dir for inlined imports; ignored when --inline-imports is not present",

"-i",
"--inline-imports",
dest="inline",
help="Flatten source into one file before compiling",

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.

Suggested change
help="Flatten source into one file before compiling",
help="flatten source into one file before compiling",

"-i",
"--inline-imports",
dest="inline",
help="Flatten source into one file before downloading",

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.

Suggested change
help="Flatten source into one file before downloading",
help="flatten source into one file before downloading",

parser.add_argument(
"--import-path",
dest="importbase",
help="Additional base dir for inlined imports. Ignored unless --inline-imports is True",

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.

Suggested change
help="Additional base dir for inlined imports. Ignored unless --inline-imports is True",
help="additional base dir for inlined imports; Ignored when --inline-imports is not present",

John Daniels and others added 3 commits June 1, 2021 12:49
@laurensvalk

Copy link
Copy Markdown
Member

Module imports are now supported by the firmware, so we will close this pull request. Thanks for submitting!

@laurensvalk laurensvalk closed this Sep 2, 2022
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