FEM Python coding standard

About the development of the FEM module/workbench.

Moderator: bernd

User avatar
PrzemoF
Veteran
Posts: 3520
Joined: Fri Jul 25, 2014 4:52 pm
Contact:

Re: Python coding standard [WORK IN PROGRESS]

Post by PrzemoF »

Arch and Draft use something similar, but there is some CMakeLists.txt rework required to do it.
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Python coding standard [WORK IN PROGRESS]

Post by DeepSOIC »

microelly2 wrote:I had some requirements for handling macros and want remember this here
A macro should have at least theese meta information for autogeneration

Code: Select all

__Comment__ = 'Imports and scales an Airfoil in the form of a Draft Wire (DWire) or Basic Spline (BSpline)'
__Web__ = "http://forum.freecadweb.org/viewtopic.php?f=22&t=5554"
__Wiki__ = "http://www.freecadweb.org/wiki/index.php?title=Macro_Airfoil_Import_%26_Scale"
__Icon__  = "/usr/lib/freecad/Mod/plugins/icons/airfoil.png"
__Help__ = "start the macro and follow the instructions"
__Author__ = "quick61"
__Version__ = '2.1'
__Status__ = 'stable'
__Requires__ = 'freecad 0.14.3706'
__Communication__ = 'your email adress or yourgit or something else'
backref: viewtopic.php?f=21&t=10905&start=20#p91086
If I have a workbench, where should that info be placed? in initGui.py?
User avatar
makkemal
Posts: 395
Joined: Wed Apr 29, 2015 12:41 pm
Location: South Africa
Contact:

Re: Python coding standard [WORK IN PROGRESS]

Post by makkemal »

We also have the issue of how does a person working only in python add an icon to the toolbar.
I did it with the following code in InitGui.py I'm not sure if a better option exists ?

Code: Select all

class FemWorkbench(Workbench):
    Icon = FreeCAD.getResourceDir() + "Mod/Fem/Resources/icons/preferences-fem.svg"
    MenuText = "FEM Disp"
    ToolTip = "FEM workbench"
    def Initialize(self):
	import Fem
        import FemGui
        import subprocess
        from platform import system
        import prescribedDisplacement
        ccx_path = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Fem").GetString("ccxBinaryPath")
        if not ccx_path:
            try:
                if system() == 'Linux':
                    p1 = subprocess.Popen(['which', 'ccx'], stdout=subprocess.PIPE)
                    if p1.wait() == 0:
                        ccx_path = p1.stdout.read().split('\n')[0]
                elif system() == 'Windows':
                    ccx_path = FreeCAD.getHomePath() + 'bin/ccx.exe'
                FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Fem").SetString("ccxBinaryPath", ccx_path)
            except Exception as e:
                FreeCAD.Console.PrintError(e.message)
	
	
	cmdList = ["Fem_NewMechanicalAnalysis",
		    "Fem_CreateFromShape",
		    "Fem_MechanicalMaterial",
		    "Separator"             ,
		    "Fem_CreateNodesSet"    ,
		    "Separator"             ,
		    "Fem_ConstraintFixed"    ,
		    "Fem_PrescribedDisplacement",
		    "Fem_ConstraintForce"    ,
		    "Fem_ConstraintPressure" ,
		    "Fem_ConstraintBearing"  ,
		    "Fem_ConstraintGear"     ,
		    "Fem_ConstraintPulley"    ,
		    "Separator"                  ,
		    "Fem_MechanicalJobControl"    ,
		    "Fem_Quick_Analysis"          ,
		    "Fem_Frequency_Analysis"      ,
		    "Fem_PurgeResults"             ,
		    "Fem_ShowResult"]
	
	self.appendToolbar("FEM", cmdList)
	self.ToolTip = "FEM workbench"
	self.appendMenu("FEM", cmdList)
	FreeCADGui.addIconPath(FreeCAD.getResourceDir() + "Mod/Fem/Resources/icons/preferences-fem.svg")
	
    def ContextMenu(self, recipient):
	selection = [s  for s in FreeCADGui.Selection.getSelection() if s.Document == FreeCAD.ActiveDocument ]
	if len(selection) == 1:
	    obj = selection[0]
	    if hasattr(obj,'Content'):
		if 'PrescribedDisplacement' in obj.Content:
		    self.appendContextMenu("Edit Constaint", "PrescribedDispEdit")

Gui.addWorkbench(FemWorkbench())
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Python coding standard [WORK IN PROGRESS]

Post by microelly2 »

DeepSOIC wrote:
microelly2 wrote:I had some requirements for handling macros and want remember this here
A macro should have at least theese meta information for autogeneration
backref: viewtopic.php?f=21&t=10905&start=20#p91086
If I have a workbench, where should that info be placed? in initGui.py?
My first focus was macros, but of course for community workbenches we can and will use InitGui.py.
qingfeng.xia
Posts: 227
Joined: Tue Sep 22, 2015 1:47 pm
Location: Oxford UK/Shenzhen China
Contact:

Re: Python coding standard [WORK IN PROGRESS]

Post by qingfeng.xia »

I am glad to see this standard.

in addition: "All public API exported to other mod should following pyQt style function name like setSomeProperty()
makeMechanicalAnalysis() is an example, if it is goning to be used somewhere else"
Ubuntu 18.04 LTS 64bit, python3, always work with latest FreeCAD daily build
Working on Cfd module for FreeCAD, FreeCAD_Module_Develop_Guide
https://github.com/ukaea/parallel-preprocessor/
https://github.com/qingfengxia/Cfd
User avatar
PrzemoF
Veteran
Posts: 3520
Joined: Fri Jul 25, 2014 4:52 pm
Contact:

Re: Python coding standard [WORK IN PROGRESS]

Post by PrzemoF »

qingfeng.xia wrote:I am glad to see this standard.

in addition: "All public API exported to other mod should following pyQt style function name like setSomeProperty()
makeMechanicalAnalysis() is an example, if it is goning to be used somewhere else"
I think that FreeCAD no longer uses pyQt and I really don't like the camel style.

1. You have a variable called someProperty
2. Functions to get/set the value are called setSomeProperty and getSomeProperty
3. Try to seach for someProperty in the code - it will not show you getSomeProperty and setSomeProperty because of the capital letters.

The variable should be named some_property and then get_some_property is more readable than getSomeProperty. You can search for it in the code without worrying if there is a capital letter or not.

PEP8: "Function names should be lowercase, with words separated by underscores as necessary to improve readability. mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility. "

I'm trying to remove all mixedCase names for FEM wb code

P.S. I'm not an official FreeCAD dev, so feel free to ignore my opinions.
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Python coding standard [WORK IN PROGRESS]

Post by bernd »

@przemo: Since you are moderator, couln't you make this thread sticky in FEM could you?
galou_breizh
Posts: 436
Joined: Wed Sep 15, 2010 9:38 am

Re: Python coding standard [WORK IN PROGRESS]

Post by galou_breizh »

Just my two cents.

getSomeProperty and setSomeProperty are not Pythonic, this is C-style. In Python, one should used properties (@property and @parameter.setter decorators for a property called parameter in new-style classes.

Gaël
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Python coding standard [WORK IN PROGRESS]

Post by bernd »

most module namesnames in FEM start with underline. Is there a special reason for this?
User avatar
PrzemoF
Veteran
Posts: 3520
Joined: Fri Jul 25, 2014 4:52 pm
Contact:

Re: Python coding standard [WORK IN PROGRESS]

Post by PrzemoF »

Internal modules that are not supposed to be used in scripting. It doesn't look nice and I'm not convinced that it's right, but that's the meaning. Probably we are not very consistent with that naming.
Post Reply