Closed Thread
Results 1 to 6 of 6

Thread: modular programming problem

  1. #1
    kakarukeys is offline Newbie
    Join Date
    Apr 2009
    Posts
    20
    Rep Power
    0

    modular programming problem

    my code has a number of classes, each of them have its own methods to communicate with users, via a console or GUI.

    Is it possible to group the methods of the same type into a module, so that, according to user preference, the program may communicate with users via a console or GUI by importing the correct module that contains all the methods (either console or GUI) for the classes?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    manux's Avatar
    manux is offline Programming Professional
    Join Date
    Oct 2008
    Posts
    234
    Blog Entries
    1
    Rep Power
    14

    Re: modular programming problem

    Well, if I understand your question, lets say you have two modules, first is name consoleUI.py and second is name GUI.py, in your main program, you just do:
    Code:
    if user_pref_ui=="c":#"c" for console
      import consoleUI as UI
    if user_pref_ui=="g":#g for graphical:
      import GUI as UI
    Of course both module need to have the same function names, so during your main program you just call lets say UI.giveData(data), and each module handles the data its own way.

  4. #3
    kakarukeys is offline Newbie
    Join Date
    Apr 2009
    Posts
    20
    Rep Power
    0

    Re: modular programming problem

    There is a twist, in my program I have already grouped the connected data and functions into objects. Each object has its own (using the example you gave) "giveData" method. I would like to group all such "giveData" methods across different objects into one module, so that, I could either import all "giveData" in consoleUI version or GUI version depending on user preference. Does Python allow this?

    If I don't do this, each class will have to have two versions of "giveData" in the memory, consoleUI and GUI.

  5. #4
    manux's Avatar
    manux is offline Programming Professional
    Join Date
    Oct 2008
    Posts
    234
    Blog Entries
    1
    Rep Power
    14

    Re: modular programming problem

    hmm... I don't really understand. But I would answer:
    Code:
    from module import Object1,Object2,...
    and If you want to change the names:
    Code:
    from module import obj1 as name1,obj2 as name2, obj3 as name3,etc...
    It will only load these specific objects in memory.

  6. #5
    kakarukeys is offline Newbie
    Join Date
    Apr 2009
    Posts
    20
    Rep Power
    0

    Re: modular programming problem

    Here is one way I can think of:
    Is there a better way?

    a.py
    Code:
    def giveDataA(self, x): #method of Class A
    	return x**2
    def giveDataB(self, x): #method of Class B
    	return x**3
    giveData = [giveDataA, giveDataB]
    b.py
    Code:
    def giveDataA(self, x): #method of Class A
    	return x**4
    def giveDataB(self, x): #method of Class B
    	return x**5
    giveData = [giveDataA, giveDataB]
    main.py
    Code:
    user_pref_ui = input()
    
    if user_pref_ui=="a": #choose A
      import a as UI
    if user_pref_ui=="b": #choose B
      import b as UI
    
    class A:
        def __init__(self):
            self.var = 5
        giveData = UI.giveData[0]
    
    class B:
        def __init__(self):
            self.var = 10
        giveData = UI.giveData[1]
    
    Oa = A()
    Ob = B()
    print(Oa.giveData(Oa.var))
    print(Ob.giveData(Ob.var))

  7. #6
    kiddies is offline Programmer
    Join Date
    May 2009
    Posts
    129
    Rep Power
    0

    Re: modular programming problem

    nice thanks....

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Should I use define or array for my modular system?
    By lol33d in forum PHP Development
    Replies: 2
    Last Post: 10-12-2011, 06:56 PM
  2. Help with a Programming problem
    By king_koder in forum C and C++
    Replies: 4
    Last Post: 10-07-2010, 05:32 AM
  3. Replies: 2
    Last Post: 12-14-2009, 02:57 AM
  4. Modular Arithmetic
    By knightry in forum Java Help
    Replies: 3
    Last Post: 07-01-2009, 04:24 PM
  5. programming or virus problem
    By mukul in forum C and C++
    Replies: 2
    Last Post: 09-30-2007, 07:33 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts