wiki:CodingStandard

Version 4 (modified by lawrence, 13 years ago) (diff)

--

Python Coding Standards for NDG

  • See the ndg.utils page for links to best practice in logging, error handling, config file

usage, xml and date handling.

Documentation Standards

In all scripts we should make the best use of module level doc strings as well as method and class strings.

e.g. for module.py

 '''some stuff about module '''
  def exitNicely(msg):
     ''' here is a method string: this method produce a nice informative exit message ...
     print __doc__
     print msg
     sys.exit()

General Coding Level Standards

  • All classes should start with a capital
  • All methods and functions should not start with a capital
  • packages should not start with a capital
  • use CamelCase for anything that is exposed ...
  • methods with intent local should be ._blah
  • methods to be not exposed should be .blah (recognising that subclasses would not be able to use blah).
  • all methods should have a docstring
  • we will use epydoc

Python Guidance

Usefile ideas:

For line length:

 a=b+c+d # or
 a=''.join([b,c
   d]) # or
 a=(b c
    d)
 a = ('the cat '
     'sat '
     'on the mat')

This is nice too:

print >> sys.stderr,stuff goes to error ...

And this is an abomination:

y = (x>10) and 5 or 7

will make y 5 if x>10 ...