0

I have seperated my GUI code(ui.py) from the code that does calculations(calcs.py) based on user's GUI entries/selections. Basically, you enter numbers and make selections and then hit start, start button should start calculations and print out stuff. Below are explanations provided with relevant parts of my code:

  1. When you hit the button, below method starts:

    import calcs
    class Application:
        def sendGUIinput(self):
            self.entry1   = self.coeff1.get() #get entry 1
            self.entry2   = self.coeff2.get() #get entry 2
            self.entry3   = self.coeff3.get() #get entry 3
            calcs.CreateData(self.entry1, self.entry2) #pass entries 1 and 2 to calcs.py for calculations
    

Please note that, app = Application(root)

  1. calcs.CreateData does some calculations and then it calls for another class for some other calculations:

     class CreateData:
         def __init__(self, entry1, entry2):
             """some calculations here produce self.someNewVar1 and self.someNewVar2"""
             CreateData2(self.someNewVar1, self.someNewVar2)
    
  2. This is where I'm having this problem. calcs.CreateData2 takes 2 variables from calcs.CreateData, however, it also needs entry3 from Application.sendGUIinput(). I don't want to pass all the entries to CreateData and then from CreateData to CreateData2. What I am trying to do is that, having a pool of entries from ui.py in calcs.py so that after I press the button, classes in calcs.py can work without needing ui.py. I'll be grateful if you can help me with this. Thank you in advance.

Note: I define calcs.CreateData2 like;

   class CreateData2:
       def __init__(self, var1, var2):

So it accepts 2 arguments other than self but also needs entry3 from ui.py

Deniz
  • 509
  • 7
  • 26
  • 2
    "I don't want to pass all the entries to CreateData and then from CreateData to CreateData2." But this is exactly what you should do. Everything else is just making it more complicated than needed. – Cu3PO42 Jun 01 '15 at 09:46
  • My question to @Cu3PO42: Passing all the entries to every single class I use would easily work here and make my code simple, you are right. But say I need to get 100 or 1000 entries from the user and want to have different classes that work with different groups of this high number of entries. How would I define my classes then? Of course, if I had onle one .py file that would be easy but what if I wanna achieve this task using multiple .py files? Thank you for your response. – Deniz Jun 01 '15 at 10:42
  • My question to @Scironic: I understand. Can you tell me more about how I can arrange my code so it can have easy inheritances? Does the way my current code structure look complicated/impractical? Thinking of a pool of information and some number of classes that take specific information from that pool looks quite simple to me actually. Question is, how is that achieved in Python using multiple .py files. Thank you for answering. – Deniz Jun 01 '15 at 10:45
  • @Deniz, I misunderstood your question so deleted my comment. See answer below. – NDevox Jun 01 '15 at 10:57

1 Answers1

0

So I'm going to say that I am largely in agreement with @Cu3PO42. If you have large numbers of entries coming which could be of arbitrary sizes, then use the *args keyword to unpack it and then make sure your classes can deal with the arbitrary sizes for their calculations.

I think that is honestly the best way forward if it's purely about size.

If there is more detail required, you want to pool data and store it, or some other complexity we are not sure about. You could use an intermediate DB and create classes which query the DB. SQL or NoSQL are the most likely candidates for what you may need depending on the specifics. If you don't want to delve into a program using multiple languages (although Python works quite well with SQL and NoSQL) you could instead use tabular files (csv), standard stored data formats (json) or serialised Python objects (pickle). All of which are built in to Python already.

Community
  • 1
  • 1
NDevox
  • 4,056
  • 4
  • 21
  • 36
  • I think what I need to do is as you and Cu3PO42 said is just pass all variables onto a single class in my calcs.py and then instead of defining classes for different steps of calculations, I'll just define different methods in one single class in that seperate .py file. Since I have only 12 arguments, getting into SQL or trying to create and run an external DB would be just extra work. I appreciate your and Cu3PO42's answers. – Deniz Jun 01 '15 at 14:29