Hardcore Programming For Mechanical Engineers: Build Engineering Applications from Scratch by Ángel Sola Orbaiceta

Hardcore Programming For Mechanical Engineers: Build Engineering Applications from Scratch by Ángel Sola Orbaiceta

Author:Ángel Sola Orbaiceta
Language: eng
Format: epub, mobi
Publisher: No Starch Press, Inc.
Published: 2021-06-15T00:00:00+00:00


Wrapping the App with a Script

For what we just saw in the previous section, every package that our main.py script needs to run should be accessible either from the working directory or from any other path listed in sys.path.

NOTE

Remember that the working directory is where the executing file (in this case main.py) is located.

Apart from appending paths to sys.path inside our Python code, we can also include paths in an environment variable: PYTHONPATH. When a Python script is run, it includes all paths defined in PYTHONPATH inside its sys.path.

We can therefore create a bash script at the project’s top level, which sets the right paths in PYTHONPATH and then executes our app’s main.py. Remember that we use bash scripts to group a set of command line statements and run them together by executing a single file (revisit Chapter 3 for a refresher).

At the top level of the project (at the same level as geom2d or apps), create a new file named cifpts.sh (an abbreviation of “circle from points”). In it, write the line in Listing 9-19.

PYTHONPATH=$PWD python3 apps/circle_from_points/main.py

Listing 9-19: Wrapper script

The first thing we do in this line is define an environment variable PYTHONPATH with a value set to the current directory; the current directory is stored inside another Unix environment variable: PWD.

Then, in the same line, we run main.py in apps/circle_from_points. Having the definition of PYTHONPATH in the same line where the script is run scopes the environment variable to the execution of the script only. This means that once the script is done, the variable doesn’t exist anymore.

Let’s try running the script from the shell passing the file test.txt:

$ bash cifpts.sh < apps/circle_from_points/test.txt

That should’ve printed the SVG output to the shell. We can even make the bash script appear as an executable by changing its user rights:

$ chmod +x cifpts.sh

This allows us to further simplify the execution:

$ ./cifpts.sh < apps/circle_from_points/test.txt

Remember that the output needs to be redirected to a file if we want the result written to it instead of being printed to the shell:

$ ./cifpts.sh < apps/circle_from_points/test.txt > result.svg

This looks more like something we want to share with our friends, all of whom have longed to have a script that computes the circle that passes through any three points.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.