pep 8 for material module

A forum to discuss the implementation of a good Materials system in FreeCAD
User avatar
bernd
Posts: 6923
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

pep 8 for material module

Postby bernd » Mon Jun 11, 2018 6:49 pm

Python code in material module is according pep 8 style guide. To test simply run the following on a shell in FreeCAD source tree :

Code: Select all

$ find src/Mod/Material/ -name "*\.py" | grep -v InitGui.py | xargs -I [] flake8 --ignore=E501,E265,E266,E402 []
ATM it gives the following errors, which are IMHO related to Py3, because flak8 in Python27 gives no errors.

Code: Select all

src/Mod/Material/MaterialEditor.py:163:31: F821 undefined name 'unicode'
src/Mod/Material/MaterialEditor.py:279:28: F821 undefined name 'unicode'
src/Mod/Material/importFCMat.py:81:29: F821 undefined name 'unicode'
src/Mod/Material/importFCMat.py:124:29: F821 undefined name 'unicode'
See for example:
https://github.com/FreeCAD/FreeCAD/blob ... or.py#L163

Has this to do with Py3 Py2 ? What is best to be compatible with both Py2 and Py3

bernd

- Best to test after git commit 1051f104
- to open the material editor, go to FEM, make a new document, menu model, materials, material editor, or just click on the material editor icon
- in the material editor you can load and save material cards

I wonder if this works with Py3 ?
looo
Posts: 2237
Joined: Mon Nov 11, 2013 5:29 pm

Re: pep 8 for material module

Postby looo » Mon Jun 11, 2018 7:56 pm

Python code in material module is according pep 8 style guide. To test simply run the following on a shell in FreeCAD source tree :
good idea!

bernd wrote:See for example:

Code: Select all

        elif isinstance(data, unicode):
            k = str(data)
            if k:
                if k in self.cards:
                    import importFCMat
                    d = importFCMat.read(self.cards[k])
                    if d:
			self.updateContents(d)

For python3 it's best to use unicode (normal string in python3) wherever possible. In this example, there is a conversation of unicode to bytes.

If self.cards uses default string (unicode for python3), you can skip the unicode->bytes conversation. If not you have to use bytes(data, 'utf-8') or data.encode('utf-8') instead of str(data).

Maybe trying something like this:

Code: Select all

# somewhere define unicode for python3
if sys.version_info.major >= 3:
    unicode = str
.....
.....
        elif isinstance(data, unicode):
            k = str(data)  # py2: unicode ->bytes, py3: unicode -> unicode
            if k:
                if k in self.cards:   # py3: assuming self.cards has unicode keys
                    import importFCMat
                    d = importFCMat.read(self.cards[k])  # does this function need bytes??
                    if d:
			self.updateContents(d)
I guess it's easier to test it directly...
User avatar
bernd
Posts: 6923
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: pep 8 for material module

Postby bernd » Tue Jun 12, 2018 5:18 am

looo wrote:
Mon Jun 11, 2018 7:56 pm
Python code in material module is according pep 8 style guide. To test simply run the following on a shell in FreeCAD source tree :
good idea!
in FEM we have been using this for two years already ...

how about this one ...
https://github.com/berndhahnebach/FreeC ... ff=unified
looo
Posts: 2237
Joined: Mon Nov 11, 2013 5:29 pm

Re: pep 8 for material module

Postby looo » Tue Jun 12, 2018 7:05 am

using six is maybe a good idea. I don't know if there is a best practice regarding unicode/bytes py2/py3. It always depends on the return types. Qt normally returns unicode. So you should try to use it directly (without conversation) in python3. For Python2 (if string is used on python side) you will have to encode the unicode to bytes...
It's difficult to say how this has to look for python3 without testing..., I have no development environment of freecad currently.