aodn / data-services

Scripts which are used to process incoming data in the data ingestion pipeline
GNU General Public License v3.0
1 stars 4 forks source link

Python coding style and consistency #283

Open mhidas opened 8 years ago

mhidas commented 8 years ago

Now that more of us are starting to write more Python code, it might be helpful to converge a bit in terms of coding style. In particular, naming conventions for variables, functions, classes and files. It just makes the code a bit more readable, so it's worth putting a bit of effort (not heaps) into it. As this detailed style guide for Python code suggests "A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is most important. "

My own code needs some cleaning up, but first we should agree on what to aim for. I propose we adopt the following conventions as an ideal:

    def do_something(arg):
        """Do something with arg and return the result.
        If no argument given, do nothing.
        """"

These are just suggestions, open for discussion.

danfruehauf commented 8 years ago

:+1:

What about unit tests? There are some kicking around, would be good to say something about that too.

My share of python is the ACORN Current Generator and I promise to align it to the code conventions specified here (mostly renaming function names).

mhidas commented 8 years ago

@lbesnard @bpasquer @ggalibert Any thoughts on this?

bpasquer commented 8 years ago

:+1:

ggalibert commented 8 years ago

Sounds good to me.

lbesnard commented 8 years ago

FIY : a bit faster with vim http://vim.wikia.com/wiki/Converting_variables_to_or_from_camel_case

danfruehauf commented 8 years ago

Module names (i.e. names of .py files) should be lower_case, with underscores if necessary Class names should use CapWords (capitalise first letter of every word, no underscores)

I think those 2 are contradicting somewhat. We're not using classes much, but image I have a modules called first_module.py and in it I have a class called SomeClass with a function called this_is_a_function. The result python code will look like:

import first_module # OK

first_module.SomeClass.this_is_a_function # AWKWARD

@mhidas I'd like to have your feedback on that...

lbesnard commented 8 years ago

a fair bit of work to do in data-services/lib/python/* if conventions need to be followed. until then it's not really possible to use these functions in different scripts at they are likely to be modified

danfruehauf commented 8 years ago

a fair bit of work to do in data-services/lib/python/* if conventions need to be followed. until then it's not really possible to use these functions in different scripts at they are likely to be modified

Bit by bit.

mhidas commented 8 years ago

@danfruehauf

first_module.SomeClass.this_is_a_function # AWKWARD

Personally I don't have a problem with this. For what it's worth, it does help distinguish a class from a module or sub-module. You can avoid having to write all that by importing it like this instead:

from first_module import SomeClass

Or would you prefer to use CapWords for module names as well?

danfruehauf commented 8 years ago

Or would you prefer to use CapWords for module names as well?

I just prefer to have consistency. I'm not bothered by any of that :)

mhidas commented 8 years ago

A minor point in the above mentioned style guide:

Note: When using abbreviations in CapWords, capitalize all the letters of the abbreviation.

So for example instead of AnmnFileClassifier I will have ANMNFileClassifier. Just a bit easier to read.

danfruehauf commented 8 years ago

So for example instead of AnmnFileClassifier I will have ANMNFileClassifier. Just a bit easier to read.

Perhaps update the issue above with that. It makes perfect sense.

mhidas commented 8 years ago

@danfruehauf Done