Python/it: Difference between revisions
(Created page with "Questi documenti presentano le spiegazioni in modo più intuitivo: * [https://realpython.com/python-pep8/ How to Write Beautiful Python Code With PEP 8] * [https://realpython.com/documenting-python-code/ Documenting Python Code: A Complete Guide].") |
(Created page with "== Convenzioni == In questa documentazione dovrebbero essere seguite alcune convenzioni per gli esempi Python.") |
||
Line 22: | Line 22: | ||
* [https://realpython.com/documenting-python-code/ Documenting Python Code: A Complete Guide]. |
* [https://realpython.com/documenting-python-code/ Documenting Python Code: A Complete Guide]. |
||
== |
== Convenzioni == |
||
In questa documentazione dovrebbero essere seguite alcune convenzioni per gli esempi Python. |
|||
In this documentation, some conventions for Python examples should be followed. |
|||
This is a typical function signature |
This is a typical function signature |
Revision as of 10:21, 27 August 2023
Descrizione
Python è un linguaggio di programmazione di alto livello per scopi generali, comunemente utilizzato in applicazioni di grandi dimensioni per automatizzare alcune attività creando script o macro.
In FreeCAD, il codice Python può essere utilizzato per creare vari elementi in modo programmatico, senza la necessità di fare clic sull'interfaccia utente grafica. Inoltre, molti strumenti e ambienti di lavoro di FreeCAD sono programmati in Python.
Vedere Introduzione a Python per conoscere il linguaggio di programmazione Python, quindi Guida agli Script Python e Script di base per FreeCAD per iniziare a creare script in FreeCAD .
Leggibilità
La leggibilità del codice Python è uno degli aspetti più importanti di questo linguaggio. L'uso di uno stile chiaro e coerente all'interno della comunità Python facilita i contributi da parte di diversi sviluppatori, poiché i programmatori Python più esperti si aspettano che il codice sia formattato in un certo modo e segua determinate regole. Quando si scrive codice Python, è consigliabile seguire PEP8: Style Guide for Python Code e PEP257: Docstring Conventions.
Questi documenti presentano le spiegazioni in modo più intuitivo:
Convenzioni
In questa documentazione dovrebbero essere seguite alcune convenzioni per gli esempi Python.
This is a typical function signature
Wire = make_wire(pointslist, closed=False, placement=None, face=None, support=None)
- Arguments with key-value pairs are optional, with the default value indicated in the signature. This means that the following calls are equivalent:
Wire = make_wire(pointslist, False, None, None, None)
Wire = make_wire(pointslist, False, None, None)
Wire = make_wire(pointslist, False, None)
Wire = make_wire(pointslist, False)
Wire = make_wire(pointslist)
- In this example the first argument doesn't have a default value so it should always be included.
- When the arguments are given with the explicit key, the optional arguments can be given in any order. This means that the following calls are equivalent:
Wire = make_wire(pointslist, closed=False, placement=None, face=None)
Wire = make_wire(pointslist, closed=False, face=None, placement=None)
Wire = make_wire(pointslist, placement=None, closed=False, face=None)
Wire = make_wire(pointslist, support=None, closed=False, placement=None, face=None)
- 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 FreeCAD 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 FreeCAD 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")
- FreeCAD scripting: Python, Introduction to Python, Python scripting tutorial, FreeCAD Scripting Basics
- Modules: Builtin modules, Units, Quantity
- Workbenches: Workbench creation, Gui Commands, Commands, Installing more workbenches
- Meshes and Parts: Mesh Scripting, Topological data scripting, Mesh to Part, PythonOCC
- Parametric objects: Scripted objects, Viewproviders (Custom icon in tree view)
- Scenegraph: Coin (Inventor) scenegraph, Pivy
- Graphical interface: Interface creation, Interface creation completely in Python (1, 2, 3, 4, 5), PySide, PySide examples beginner, intermediate, advanced
- Macros: Macros, How to install macros
- Embedding: Embedding FreeCAD, Embedding FreeCADGui
- Other: Expressions, Code snippets, Line drawing function, FreeCAD vector math library (deprecated)
- Hubs: User hub, Power users hub, Developer hub