Beginner question: module organisation

Discussion in 'Python' started by Mail.To.Nathaniel@gmail.com, May 14, 2007.

  1. Guest

    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?
     
    , May 14, 2007
    #1
    1. Advertising

  2. On May 14, 9:09 am, 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
     
    Nick Vatamaniuc, May 14, 2007
    #2
    1. Advertising

  3. Rainer Grimm Guest

    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?
    >

    Hello.

    Have a look at the classical GangOfFour design pattern book. You can
    especially with the template methode design the processing of your data.
    The strategy pattern will help you to vary the algroithm in your processing.

    To be concret, in your base class you define the processing of the data.
    There are two distinct methods to do it.
    delegate the variation of the algorithmns to other objects => strategy
    pattern
    override the steps of the processing in subclasses => template method

    Regards,
    Rainer
    --
    _________________________creating IT solutions
    Rainer Grimm
    scVENUS Schulungsleiter science + computing ag
    phone +49(0)7071 9457-253 Hagellocher Weg 73
    fax +49(0)7071 9457-511 D-72070 Tuebingen, Germany
    www.science-computing.de
     
    Rainer Grimm, May 19, 2007
    #3
  4. 7stud Guest

    On May 14, 7:09 am, 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?


    How about this:

    refineModule.py:
    ---------------
    def refine(userfunc, mesh):
    #process mesh
    func(mesh)

    aprogram.py:
    ------------
    import refineModule

    def myRefine(mesh):
    print mesh

    refineModule.refine(myRefine, "hello world")
     
    7stud, May 19, 2007
    #4
  5. 7stud Guest

    On May 19, 10:45 am, 7stud <> wrote:
    >
    > refineModule.py:
    > ---------------
    > def refine(userfunc, mesh):
    > #process mesh
    > func(mesh)
    >


    The last line should be:

    userfunc(mesh)
     
    7stud, May 19, 2007
    #5
  6. Guest

    Thank you for all your help!

    I'll study the proposals to chose the one I prefer or to create
    something new :) Anyway, this is a perfect start-up for me.

    Nathaniel.
     
    , Jun 5, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Noozster

    WebForm Organisation

    Noozster, Feb 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    363
    =?Utf-8?B?TGFycnkgQ2hhcmx0b24=?=
    Feb 27, 2006
  2. mateo
    Replies:
    3
    Views:
    813
    Tim_Mac
    Mar 3, 2006
  3. SophistiCat

    Advice on input organisation

    SophistiCat, May 20, 2005, in forum: C++
    Replies:
    2
    Views:
    391
    SophistiCat
    May 23, 2005
  4. David M. Wilson

    Package organisation question.

    David M. Wilson, Feb 6, 2004, in forum: Python
    Replies:
    0
    Views:
    296
    David M. Wilson
    Feb 6, 2004
  5. Stuart Turner

    Getting Python Accepted in my Organisation

    Stuart Turner, Nov 3, 2005, in forum: Python
    Replies:
    30
    Views:
    750
    Steven D'Aprano
    Nov 5, 2005
Loading...

Share This Page