Difference between revisions of "Python"

From Healthcare Robotics Wiki
Jump to: navigation, search
(HRL Coding Standards)
(Call Matlab from Python)
 
(17 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
== HRL Coding Standards ==
 
== HRL Coding Standards ==
* In general, we follow the official python coding standards ([http://www.python.org/dev/peps/pep-0008/ Python standards]) but we will summarize here the relevant key points.
+
In general, we '''strongly''' recommend that everyone follow the official python coding standards ([http://www.python.org/dev/peps/pep-0008/ Python standards]) but we will summarize here the relevant key points.
** Every indent is 4 spaces, replace '''all''' tabs with spaces.
+
* Every indent is 4 spaces, replace '''all''' tabs with spaces.
** ClassName, method_name, _private_name
+
* Don't use 'from blah_module import *'
** Use classes sparingly, please try to define functions whenever possible.  Functions should not be more than a page or two long unless there is an extremely good reason to do so.
+
* imported names must be longer than 1 character.
** Instead of using inheritance, use [http://en.wikipedia.org/wiki/Duck_typing duck typing] (i.e. define interfaces based on conventions)
+
* all imports should be at the top of your file except for..
** With numpy, use matrices instead of arrays, all vectors should be column vectors.
+
* imports that are only used in your __main__ blocks should be at the beginning of that __main__ block.
** We use doxygen styled comments:
+
* ClassName, method_name, _private_name
 +
* Use default parameters sparingly.
 +
* Use classes sparingly, please try to define functions whenever possible.  Functions should not be more than a page or two long unless there is an extremely good reason to do so.
 +
* Instead of using inheritance, use [http://en.wikipedia.org/wiki/Duck_typing duck typing] (i.e. define interfaces based on conventions)
 +
* With numpy, use matrices instead of arrays, all vectors should be column vectors.
 +
* Debugging printouts that are left in the code should be annotated with the class and method name that is generating it.  Instead of:
 +
  def some_func(a,b):
 +
    print "results of a + b", a+b
 +
Use:
 +
  def some_func(a,b):
 +
    print "<my_file.py> some_func: results of a+b
 +
* We use doxygen styled comments:
 
   ##
 
   ##
 
   # some comments
 
   # some comments
Line 14: Line 25:
 
   def my_function(a, b):
 
   def my_function(a, b):
 
       return b*a
 
       return b*a
** Use default parameters sparingly
+
* Use whitespace to separate functional parts of code, but '''do not''' use them misleadingly.  This is misleading:
** We use python 2.5.2
+
** Use whitespace to separate functional parts of code, but '''do not''' use them misleadingly.  This is misleading:
+
 
         self.size_image = highgui.cvQueryFrame(self.capture)
 
         self.size_image = highgui.cvQueryFrame(self.capture)
         self.size = cv.cvGetSize( self.size_image )
+
         self.size = cv.cvGetSize( self.size_image )<br>
 
+
         self.color = cam_param['color']<br>
         self.color = cam_param['color']
+
 
+
 
         self.intrinsic_cvmat = cv.cvCreateMat(3,3,cv.CV_32FC1)
 
         self.intrinsic_cvmat = cv.cvCreateMat(3,3,cv.CV_32FC1)
 
         self.distortion_cvmat = cv.cvCreateMat(1,4,cv.CV_32FC1)
 
         self.distortion_cvmat = cv.cvCreateMat(1,4,cv.CV_32FC1)
Line 30: Line 37:
 
         self.intrinsic_cvmat = cv.cvCreateMat(3,3,cv.CV_32FC1)
 
         self.intrinsic_cvmat = cv.cvCreateMat(3,3,cv.CV_32FC1)
 
         self.distortion_cvmat = cv.cvCreateMat(1,4,cv.CV_32FC1)
 
         self.distortion_cvmat = cv.cvCreateMat(1,4,cv.CV_32FC1)
 +
* Again, we '''strongly''' recommend that you be extremely familiar with the python coding standards: [http://www.python.org/dev/peps/pep-0008/ Python standards]
  
 
== learning python ==
 
== learning python ==
 +
* We follow the [http://www.python.org/dev/peps/pep-0008/ Python coding standards].
 
* http://docs.python.org/
 
* http://docs.python.org/
 
* http://docs.python.org/library/index.html
 
* http://docs.python.org/library/index.html
Line 39: Line 48:
 
*quick reference (http://rgruet.free.fr/PQR25/PQR2.5.html)
 
*quick reference (http://rgruet.free.fr/PQR25/PQR2.5.html)
 
*ipython (http://ipython.scipy.org/moin/)
 
*ipython (http://ipython.scipy.org/moin/)
 +
** to execute a script in ipython, type: ''run [script name]''
  
 
== math, science, numerical, etc. ==
 
== math, science, numerical, etc. ==
Line 51: Line 61:
 
== efficiency ==
 
== efficiency ==
 
* When you need buffering of some sort for logging/writing data, if speed is important use the deque data structure instead of lists: http://docs.python.org/library/collections.html#collections.deque
 
* When you need buffering of some sort for logging/writing data, if speed is important use the deque data structure instead of lists: http://docs.python.org/library/collections.html#collections.deque
 +
 +
== Call Matlab from Python ==
 +
* [http://mlabwrap.sourceforge.net/ mlabwrap] - Use this to easily call Matlab from within Python.  Useful for computer vision libraries, displays, or the signal processing toolkits among the other functions which make Matlab really useful.  Interoperates well with Numpy matricies.

Latest revision as of 19:09, 2 April 2012

HRL Coding Standards

In general, we strongly recommend that everyone follow the official python coding standards (Python standards) but we will summarize here the relevant key points.

  • Every indent is 4 spaces, replace all tabs with spaces.
  • Don't use 'from blah_module import *'
  • imported names must be longer than 1 character.
  • all imports should be at the top of your file except for..
  • imports that are only used in your __main__ blocks should be at the beginning of that __main__ block.
  • ClassName, method_name, _private_name
  • Use default parameters sparingly.
  • Use classes sparingly, please try to define functions whenever possible. Functions should not be more than a page or two long unless there is an extremely good reason to do so.
  • Instead of using inheritance, use duck typing (i.e. define interfaces based on conventions)
  • With numpy, use matrices instead of arrays, all vectors should be column vectors.
  • Debugging printouts that are left in the code should be annotated with the class and method name that is generating it. Instead of:
 def some_func(a,b):
   print "results of a + b", a+b

Use:

 def some_func(a,b):
   print "<my_file.py> some_func: results of a+b
  • We use doxygen styled comments:
 ##
 # some comments
 # @param a is 3x1 point in coordinate frame C
 # @param b is a 3x3 rotation matrix
 # @return a 3x1 vector rotated by b
 def my_function(a, b):
     return b*a
  • Use whitespace to separate functional parts of code, but do not use them misleadingly. This is misleading:
       self.size_image = highgui.cvQueryFrame(self.capture)
       self.size = cv.cvGetSize( self.size_image )
self.color = cam_param['color']
self.intrinsic_cvmat = cv.cvCreateMat(3,3,cv.CV_32FC1) self.distortion_cvmat = cv.cvCreateMat(1,4,cv.CV_32FC1)

The below would be preferred if there are not obvious separations:

       self.size_image = highgui.cvQueryFrame(self.capture)
       self.size = cv.cvGetSize( self.size_image )
       self.color = cam_param['color']
       self.intrinsic_cvmat = cv.cvCreateMat(3,3,cv.CV_32FC1)
       self.distortion_cvmat = cv.cvCreateMat(1,4,cv.CV_32FC1)
  • Again, we strongly recommend that you be extremely familiar with the python coding standards: Python standards

learning python

math, science, numerical, etc.

opencv

efficiency

Call Matlab from Python

  • mlabwrap - Use this to easily call Matlab from within Python. Useful for computer vision libraries, displays, or the signal processing toolkits among the other functions which make Matlab really useful. Interoperates well with Numpy matricies.