Coding standards: Function name aliases: how to use?
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
Coding standards: Function name aliases: how to use?
In the Draft code most functions have an alias:
joinWires = join_wires
Is this for compatibility? Where is this documented? And what should you use when you import Draft in your code? In the docstrings the camelCase name is used which suggests that that is the name to choose. In the scale.py module itself functions are called by their alias, strange, but it does confirm this idea.
In gui_scale.py there are a number of functions that do not have snake_case names. For some there may be technical reasons (GetResources and Activated) but in other cases it just seems inconsistent (scaleGhost and numericInput).
Aside:
The join_wires function (join.py) is a bit strange as it makes a recursive call to itself using its alias.
joinWires = join_wires
Is this for compatibility? Where is this documented? And what should you use when you import Draft in your code? In the docstrings the camelCase name is used which suggests that that is the name to choose. In the scale.py module itself functions are called by their alias, strange, but it does confirm this idea.
In gui_scale.py there are a number of functions that do not have snake_case names. For some there may be technical reasons (GetResources and Activated) but in other cases it just seems inconsistent (scaleGhost and numericInput).
Aside:
The join_wires function (join.py) is a bit strange as it makes a recursive call to itself using its alias.
Last edited by Kunda1 on Thu Oct 08, 2020 11:41 pm, edited 1 time in total.
Reason: fixed typo in thread title
Reason: fixed typo in thread title
Re: Coding standards: Function name aliasses: how to use?
Yes. Carlopav and I reworked the Draft Workbench earlier this year, and we renamed many functions to be PEP8 compliant. But to prevent breaking workbenches that depend on Draft (Arch and BIM mostly) and older scripts, providing the old name is necessary.
The PEP8 guidelines indicate that functions should be snake_case, so this should be the "official" name; the camelCase should be for compatibility purposes only. Obviously not every single function of the workbench has been renamed. This is a work in progress and takes a long time to complete; the documentation strings need to be updated as well, of course.
By the way, Carlo used a simple alias like what you wrote above. But lately I've been using a more clever way, creating the old function from the new function. So, we can actually inject a deprecation message, and the corresponding docstring.
Code: Select all
def getSVG(obj, args):
"""Return SVG string of the object. DEPRECATED. Use 'get_svg'."""
utils.use_instead("get_svg")
return get_svg(obj, args)
Those capitalized methods inside objects (draftobjects), viewproviders (draftviewproviders) and commands (draftguitools) are re-implementations of the C++ base class methods, so they must be named exactly like that (GetResources, Activated, Deactivated, etc.). But other methods should be renamed to comply with PEP8, as long as it doesn't affect compatibility. As soon as we detect incompatibilities, we should think about clever ways of handling that....
For some there may be technical reasons (GetResources and Activated) but in other cases it just seems inconsistent (scaleGhost and numericInput).
Unfortunately, there is no rule to know which methods come from C++, you have to check the C++ sources as well.
This could be easily changed. As long as the alias is identical to the base function, it should work the same.The join_wires function (join.py) is a bit strange as it makes a recursive call to itself using its alias.
A lot of this is discussed in the big reorganization thread, [Discussion] Splitting Draft tools into their own modules.
But there is more to do. I'm going through various modules, fixing things, updating the docstrings, the algorithms where it makes sense, etc., but it takes a lot of time if you want to do it right. It's not just cosmetic issues, it's also about fixing small bugs, and other things that I notice in the code (for example, handle new objects that didn't exist before, handle App Links, etc.).
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Re: Coding standards: Function name aliasses: how to use?
I have the same question, please.Roy_043 wrote: ↑Wed Oct 07, 2020 9:19 am In the Draft code most functions have an alias:Where is this documented?Code: Select all
joinWires = join_wires
PEP8 is for Python programs, but FreeCAD is not a Python program, some of it is C++, and a lot of calls go to Qt which is also C++ : therefore, a lot of functions and calls will never follow PEP8 guidelines. This means that FreeCAD has the choice to be either fully C++ like snakeOil, or to have a mix of C++ snakeOil and PEP8 snake_oil functions. But FreeCAD will never be all PEP8 compliant.
to summarize: this is a huge work, purely cosmetic, brings confusion and no advantage. What is the purpose of it ?
Re: Coding standards: Function name aliasses: how to use?
I just can talk about FEM.
In FEM we did do this years ago. All C++ methods are camelCase als Python methods are snake_case (rigth fro the beginning so no compatibility probelems) and most code is pep8 compliant and and and. See https://github.com/FreeCAD/FreeCAD/blob ... entions.md
Why?
IMHO it is more than cosmethic. It is about consistency. If there is no unify code standard it is very very hard to read the code, to debug the code, etc. Much time in maintaining is fixing bug, fixing bugs in code other people made. If at leas this code is according a standard it is much simpler to read and to understand. Eventually this saves a lot of time.
Since we do not like to make our own standard we just used pep8.
m2c
In FEM we did do this years ago. All C++ methods are camelCase als Python methods are snake_case (rigth fro the beginning so no compatibility probelems) and most code is pep8 compliant and and and. See https://github.com/FreeCAD/FreeCAD/blob ... entions.md
Why?
IMHO it is more than cosmethic. It is about consistency. If there is no unify code standard it is very very hard to read the code, to debug the code, etc. Much time in maintaining is fixing bug, fixing bugs in code other people made. If at leas this code is according a standard it is much simpler to read and to understand. Eventually this saves a lot of time.
Since we do not like to make our own standard we just used pep8.
m2c
Re: Coding standards: Function name aliasses: how to use?
When I started with FreeCAD we even had a lot of code files which had space and tab indent mixed, 2 space and 4 space indent. Windows and Unix line endings mixed in one file. Such stuff ist just a night mare to work with. Happily in FEM this is no longer the case.
Re: Coding standards: Function name aliasses: how to use?
But any call to a FreeCAD function will be in the form of snakeOil:
Code: Select all
App.activeDocument().getObject('disk001')
Code: Select all
RotXButton.setToolTip()
columnsLayout.addLayout()
attLCSlist.setMinimumHeight()
On the other hand, using snakeOil() for functions and some_variable for variables, it's very easy to see the difference between variables and functions in your code. In this way, all calls will be in the form:
Code: Select all
myGreatFunction(some_variable)
Re: Coding standards: Function name aliasses: how to use?
Python code of FreeCAD is still Python code, it can follow standards. You aren't thinking long term. We can refactor FreeCAD to have Python compliant functions, it just needs work, vanuan already provided some proposals on how to enforce this PEP8 checking. It's not extremely hard, it's just tedious work that somebody should do. Qt itself is moving towards more compatibility with Python, they said it themselves at the beginning of the year; it's possible that PySide6 for Qt6 will include a more Python compliant interface.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Re: Coding standards: Function name aliasses: how to use?
I must admit in FEM this is mixed ...
To be honest years ago code formating was a night mare, a coding standard was needed, pep8 seamed reasonable and bump here we are ....
FEM_Tutorial_Python
To be honest years ago code formating was a night mare, a coding standard was needed, pep8 seamed reasonable and bump here we are ....
FEM_Tutorial_Python
Re: Coding standards: Function name aliasses: how to use?
in other circumstances I would have stopped reading here, but I'll make an exception:
you still didn't justify what the purpose of all this work is. All I can see is cosmetic waste of resources. And I certainly hope that no-one will allow you to waste everybody-else's time in refactoring FreeCAD code to have Python compliant functions.We can refactor FreeCAD to have Python compliant functions, it just needs work
Re: Coding standards: Function name aliases: how to use?
Just want to interject here withmy moderator hat on and ask for a little de-escalation in the tone of this important exchange of ideas and long term vision.
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs