Users (or developers!) may not realize that the shell expands glob asterisk * unless enclosed in quotes.
This can surprise users unfamiliar with this shell behavior, say when using Python argparse with position-based arguments.
Say a user has a single file to process in a directory, and doesn’t want to type the long filename, so they type:
python myScript.py ~/data/*.h5 32
Here we assume myScript.py expects two positional arguments, the first being a filename, and the second being an integer.
If more than one “*.h5” file subsequently exists and myScript.py is run, the actual input to Python would be like:
CMake ExternalProject works for many types of sub-projects across CMake generators.
An implementation detail of Ninja is by default ExternalProject doesn’t print progress until each ExternalProject step is finished.
For large external projects that take several minutes to download and build, users could be confused thinking CMake has frozen up.
To make ExternalProject show live progress as it does with Makefiles generators, add the
USES_TERMINAL_* true
arguments to ExternalProject_Add.
CMake doesn’t print the Generator version by default.
Sometimes bugs are related to a specific generator version.
Reveal CMake generator version like this
snippet.
PyTest can work with Matlab Engine if the Matlab Engine is setup.
Use a try-catch to ensure any non-functioning Matlab Engine issue is skipped.
importpytestdeftest_me():
try:
mateng = pytest.importorskip("matlab.engine")
exceptException: # can also get RuntimeError, let's just catch all pytest.skip("Matlab engine not available")
eng = mateng.start_matlab("-nojvm")
# test code
Python subprocess can be used to run a long-running program, capturing the output to a variable and printing to the screen simultaneously.
This gives the user the comfort that the program is working OK and gives program status messages without waiting for the program to finish.
This example demonstrates the “tee” subprocess behavior.
Python subprocess can run inline multi-line Python code.
This is useful to use Python as a cross-platform demonstration or for production code where a new Python instance is called.
importsubprocessimportsys# the -u is to ensure unbuffered output so that program prints livecmd = [sys.executable, "-u", "-c", r"""
import sys
import datetime
import time
for _ in range(5):
print(datetime.datetime.now())
time.sleep(0.3)
"""]
subprocess.check_call(cmd)
Matlab command batch “matlab -batch” is useful for running Matlab scripts from the command line.
When using “stdout” text output from Matlab, especially if only a single line is expected, there may be extraneous text output from Matlab with regard to licensing.
A command example is prereleases like:
matlab -batch "disp(matlabroot)"
outputs to stdout:
Prerelease License -- for engineering feedback and testing
purposes only. Not for sale.
/Applications/MATLAB_R2023b.app
A workaround for this in shell scripts is like:
set -e # stop on errorr=$(matlab -batch "disp(matlabroot)" | tail -n1)cd${r}# and so on
It can be convenient to open a file by launching the default program without first leaving the Terminal.
For simplicity, we assume the file is named “file.txt” but it can be any file openable by a program on the computer.
This technique works with any file type that has an associated default program on the computer.
Git filters may clash with the CMake ExternalProject
update step.
The “download” step invokes checkout and the “update” step may stash and invoke the Git filters, causing the build to fail.
Solution:
Git pre-commit hook
instead of Git filters.
Users with Git filters need to disable the filters and preferably change the filters to pre-commit hooks if possible.