Beginner question: module organisation

Nick Vatamaniuc vatamane at gmail.com
Mon May 14 12:10:03 EDT 2007


On May 14, 9:09 am, Mail.To.Nathan... at gmail.com wrote:
> Hello :)
>
> I am new to python and I don't have much expirience in object-oriented
> technologies neither.
>
> The problem is the following: I have to create a simple python
> template script that will always follow the same algorithm, let's say:
> - read a mesh
> - transform the mesh (let's say, refine)
>
> The last step should be a kind of a black box:
> - the same input data format
> - some algorithme inside
> - the same output data format
>
> A number of different refine methods should be implemented. The end-
> user must be able to write easily a new method and call it from the
> base script without any major change.
>
> Something like this would be a solution (no classes created, no OO
> programming):
> - a module defining REFINE1(mesh), REFINE2(mesh), ...
> - in the script:
>   from MODULE import REFINE2 as REFINE
>   REFINE(mesh)
>
> Is it a proper solution for this kind of problem? How would you
> implement this kind of task?

Why not OO? This is a good problem for OO. For example: there is a
base class (BaseMesh)  that will take care of loading your
mesh,provide a generic (possibly empty)  refine() method,  output the
mesh and have a bunch of utility functions. You can put that in a
module like meshing.py.

Then the user will do:
--------------------------------------------
from meshing import BaseMesh
class UsersMesh(BaseMesh):
    def __init__(self,...):
         BaseMesh.__init__(self,...)
         ....etc. initializer...
    def refine(self,...):
          ...user's refine method would go here...
--------------------------------------------------

So for each different refine() method the user can derive a new class
from BaseMesh and overload the refine(...) method.

Hope that helps,
-Nick Vatamaniuc





More information about the Python-list mailing list