Python coding standard

About the development of the FEM module/workbench.
User avatar
PrzemoF
Posts: 1715
Joined: Fri Jul 25, 2014 4:52 pm
Contact:

Re: Python coding standard [WORK IN PROGRESS]

Postby PrzemoF » Tue Oct 20, 2015 1:48 pm

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

Re: Python coding standard [WORK IN PROGRESS]

Postby DeepSOIC » Tue Oct 20, 2015 3:11 pm

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?
makkemal
Posts: 325
Joined: Wed Apr 29, 2015 12:41 pm

Re: Python coding standard [WORK IN PROGRESS]

Postby makkemal » Tue Oct 20, 2015 4:58 pm

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
Posts: 2450
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Python coding standard [WORK IN PROGRESS]

Postby microelly2 » Wed Oct 21, 2015 11:27 am

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: 107
Joined: Tue Sep 22, 2015 1:47 pm
Location: Oxford Uk
Contact:

Re: Python coding standard [WORK IN PROGRESS]

Postby qingfeng.xia » Sat Oct 24, 2015 9:17 pm

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 0.17-dev OpenFOAM + FreeCAD
User avatar
PrzemoF
Posts: 1715
Joined: Fri Jul 25, 2014 4:52 pm
Contact:

Re: Python coding standard [WORK IN PROGRESS]

Postby PrzemoF » Wed Oct 28, 2015 3:37 pm

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
Posts: 3924
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Python coding standard [WORK IN PROGRESS]

Postby bernd » Wed Oct 28, 2015 4:19 pm

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

Re: Python coding standard [WORK IN PROGRESS]

Postby galou_breizh » Thu Oct 29, 2015 9:19 pm

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
Posts: 3924
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Python coding standard [WORK IN PROGRESS]

Postby bernd » Wed Nov 04, 2015 8:26 pm

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

Re: Python coding standard [WORK IN PROGRESS]

Postby PrzemoF » Wed Nov 04, 2015 8:32 pm

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.