Python: Difference between revisions
(Created page with " {{VeryImportantMessage|LabRPS was originally designed to work with Python 2.x. This series ended with 2.7.18 release dated April, 20th 2020 and is succeeded by Python 3. The further development of LabRPS will be done exclusively with Python 3, and backwards compatibility will not be supported.}} == Description == {{TOCright}} [https://www.python.org Python] is a general purpose, high level programming language that is very commonly used in large applications to auto...") |
No edit summary |
||
Line 1: | Line 1: | ||
{{VeryImportantMessage|LabRPS was originally designed to work with Python | {{VeryImportantMessage|LabRPS was originally designed to work with Python 3.x and for now the further development of LabRPS will still be done exclusively with Python 3.x.}} | ||
== Description == | == Description == | ||
Line 22: | Line 22: | ||
== Conventions == | == Conventions == | ||
In this documentation, some conventions for Python examples should be followed. | In this documentation, some conventions for Python examples should be followed. | ||
* Python's guidelines stress readability of code; in particular, parentheses should immediately follow the function name, and a space should follow a comma. | * Python's guidelines stress readability of code; in particular, parentheses should immediately follow the function name, and a space should follow a comma. |
Latest revision as of 16:39, 3 February 2025
Description
Python is a general purpose, high level programming language that is very commonly used in large applications to automate some tasks by creating scripts or macros.
In LabRPS, Python code can be used to create various elements programmatically, without needing to click on the graphical user interface. Additionally, many tools and workbenches of LabRPS are programmed in Python.
See Introduction to Python to learn about the Python programming language, and then Python scripting tutorial and LabRPS Scripting Basics to start scripting in LabRPS.
Readability
Readability of Python code is one of the most important aspects of this language. Using a clear and consistent style within the Python community facilitates contributions by different developers, as most experienced Python programmers expect the code to be formatted in a certain way and to follow certain rules. When writing Python code, it is advisable to follow PEP8: Style Guide for Python Code and PEP257: Docstring Conventions.
These documents present explanations in a more user-friendly way:
Conventions
In this documentation, some conventions for Python examples should be followed.
- Python's guidelines stress readability of code; in particular, parentheses should immediately follow the function name, and a space should follow a comma.
p1 = Vector(0, 0, 0) p2 = Vector(1, 1, 0) p3 = Vector(2, 0, 0) Wire = make_wire([p1, p2, p3], closed=True)
- If code needs to be broken over several lines, this should be done at a comma inside brackets or parentheses; the second line should be aligned with the previous one.
a_list = [1, 2, 3, 2, 4, 5] Wire = make_wire(pointslist, False, None, None, None)
- Functions may return an object that can be used as the base of another drawing function.
Wire = make_wire(pointslist, closed=True, face=True) Window = make_window(Wire, name="Big window")
Imports
Python functions are stored in files called modules. Before using any function in that module, the module must be included in the document with the import
instruction.
This creates prefixed functions, that is, module.function()
. This system prevents name clashes with functions that are named the same but that come from different modules.
For example, the two functions Arch.make_window()
and myModule.make_window()
may coexist without problem.
Full examples should include the necessary imports and the prefixed functions.
import LabRPS as App import Draft p1 = App.Vector(0, 0, 0) p2 = App.Vector(1, 1, 0) p3 = App.Vector(2, 0, 0) Wire = Draft.make_wire([p1, p2, p3], closed=True)
import LabRPS as App import Draft import Arch p1 = App.Vector(0, 0, 0) p2 = App.Vector(1, 0, 0) p3 = App.Vector(1, 1, 0) p4 = App.Vector(0, 2, 0) pointslist = [p1, p2, p3, p4] Wire = Draft.make_wire(pointslist, closed=True, face=True) Structure = Arch.make_structure(Wire, name="Big pillar")

- LabRPS scripting: Python, Introduction to Python, Python scripting tutorial, LabRPS Scripting Basics
- Modules: Units, Quantity
- Workbenches: Gui Commands, Commands
- Parametric objects: Scripted objects, Viewproviders
- Graphical interface: Interface creation, Interface creation completely in Python, PySide, PySide examples beginner, intermediate, advanced
- Macros: Macros, How to install macros
- Other: Expressions
- Hubs: User hub, Power users hub, Developer hub