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
Hardcore Programming For Mechanical Engineers: Build Engineering Applications from Scratch by Ángel Sola Orbaiceta.mobi
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.
Whiskies Galore by Ian Buxton(41524)
Introduction to Aircraft Design (Cambridge Aerospace Series) by John P. Fielding(32881)
Small Unmanned Fixed-wing Aircraft Design by Andrew J. Keane Andras Sobester James P. Scanlan & András Sóbester & James P. Scanlan(32567)
Craft Beer for the Homebrewer by Michael Agnew(17926)
Turbulence by E. J. Noyes(7690)
The Complete Stick Figure Physics Tutorials by Allen Sarah(7134)
Kaplan MCAT General Chemistry Review by Kaplan(6584)
The Thirst by Nesbo Jo(6432)
Bad Blood by John Carreyrou(6270)
Modelling of Convective Heat and Mass Transfer in Rotating Flows by Igor V. Shevchuk(6218)
Learning SQL by Alan Beaulieu(6023)
Weapons of Math Destruction by Cathy O'Neil(5819)
Man-made Catastrophes and Risk Information Concealment by Dmitry Chernov & Didier Sornette(5638)
Digital Minimalism by Cal Newport;(5383)
Life 3.0: Being Human in the Age of Artificial Intelligence by Tegmark Max(5181)
iGen by Jean M. Twenge(5149)
Secrets of Antigravity Propulsion: Tesla, UFOs, and Classified Aerospace Technology by Ph.D. Paul A. Laviolette(4974)
Design of Trajectory Optimization Approach for Space Maneuver Vehicle Skip Entry Problems by Runqi Chai & Al Savvaris & Antonios Tsourdos & Senchun Chai(4837)
Electronic Devices & Circuits by Jacob Millman & Christos C. Halkias(4739)
