how does a real world program work ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • d3ph03n1x
    New Member
    • Feb 2010
    • 10

    how does a real world program work ?

    hello people,
    Im have learned python's concepts and can do a fair bit of coding in it. What I'm looking at is to create a real world GUI based program. Of which is plan to create the program text based and then convert it to GUI wxpython.

    So before I did go ahead with that I thought of clearing my concepts/doubts:
    1) what is the best way to create a program monolithic(Like in the Linux kernel all in one) or like the Micro Kernel (All servers communicating with each other to form a single program or Operating system in the case of the Mach kernel)?

    2) Are there any real world examples or open source projects based on the above, which I could be directed towards.?

    3) Is creating the programs Text based and then converting to GUI a good way to program ?

    4) looking a cross-platform GUI based program, is wxpython the right way to go?

    5) When going Menu Driven what function to use to call a file within the same folder to the corresponding choice file.

    example:

    c:\myprog\mainm enu.py
    c:\myprog\menu_ option1.py
    c:\myprog\menu_ option1.py


    print "\nMain Menu:"
    print "-----------"

    # Print out the menu:
    print "Please enter choice:"
    print "1 - menu_option1"
    print "5 - menu_option2"


    # The input function both prompts the user for input and fetches it...
    input_choice = int(input( "> " ))



    if input_choice == 1:
    print("menu_opt ion1 choosen")
    print("how do i call menu_option2.py ")
    else:
    print("menu_opt ion2 choosen")
    print("how do i call menu_option2.py ")


    Any help would be wonderful. Thanks in advance!
  • d3ph03n1x
    New Member
    • Feb 2010
    • 10

    #2
    Ok! I don't Know if the people who can help have viewed the question or not.

    but I'v found my answer to my 5th question, which is:

    temp_file_path= "c:\\myprog\\me nu_option1.py"
    temp_file_path= "c:\\myprog\\me nu_option1.py"



    This is how it would be called, though if anyone has a better suggestion it would be appreciated. As this is still static and I'm looking at a dynamic solution.


    print "\nMain Menu:"
    print "-----------"

    # Print out the menu:
    print "Please enter choice:"
    print "1 - menu_option1"
    print "5 - menu_option2"

    temp_file_path= ""

    # The input function both prompts the user for input and fetches it...
    input_choice = int(input( "> " ))
    temp_file_path= "c:\\myprog\\me nu_option1.py"
    execfile(temp_f ile_path)


    if input_choice == 1:

    else:
    temp_file_path= "c:\\myprog\\me nu_option2.py"
    execfile(temp_f ile_path)


    Thanks once again!

    Comment

    • d3ph03n1x
      New Member
      • Feb 2010
      • 10

      #3
      wow!! I'm shocked 29 Views! Not a single reply.. Neway Thought i should Clarify the 2nd question..

      2) Are there any real world examples or open source projects based on the above, which I could be directed towards?

      I know/found many on sourceforge but have'nt really found a small simple program/project. which could clear my doubts. And most projects depend on many other projects which makes it even more confusing to keep track of what's actually happening.

      Hope this clarification helps!
      Thanks! :)

      Comment

      • d3ph03n1x
        New Member
        • Feb 2010
        • 10

        #4
        No help ? Surprising! I was under the misconception that I would get a quick and helpful reply.

        Comment

        • Glenton
          Recognized Expert Contributor
          • Nov 2008
          • 391

          #5
          I don't know a huge amount about it, but the answer to question 3 is yes! It's the OO way!

          Comment

          • Glenton
            Recognized Expert Contributor
            • Nov 2008
            • 391

            #6
            Regarding your question 5, I'm not entirely sure what your specific question is. But the coding can probably be cleaned up a great deal by building up the exec file as follows (BTW please use code tags!!)

            Code:
            temp_file_path="c:\\myprog\\menu_option"+input_choice+".py"
            execfile(temp_file_path)
            You may want to check that the input_choice is valid (and I'm assuming it's a string, otherwise you need to do it slightly differently), but probably the GUI will be able to ensure that it is...

            Comment

            • d3ph03n1x
              New Member
              • Feb 2010
              • 10

              #7
              Yes Glenton, thanx the answer to Q3 is great, as i shall not be doubtfull anymore. Coming to Q5 that would'nt be the answer I would have been looking for, but then there is always something new to learn! And yes input_choice its a string.

              Actually in Q5 what i meant is I have a folder named myprog and within that folder I have 4 files.mainmenu. py, menu_option1.py , menu_option2.py , exit.py. So basically what should be happing is that mainmenu.py is Executed and is displays a menu with 3 options
              Code:
              1 - menu_option1.py
                         2 - menu_option2.py
                         x - exit
              So actually what should be happening is when the user presses "1" and enter, the program executes menu_option1.py which is an individual file in itself, In this way a particular file can be easily be maintained when dealing with large programs.


              c:\myprog\mainm enu.py
              c:\myprog\menu_ option1.py
              c:\myprog\menu_ option2.py
              c:\myprog\exit. py


              Code:
              input_choice = 1
              
              
              while input_choice != 0:
                  
                  
                  print "\n\n\n\n Main Menu:"
              
                  
              # Print out the menu:
              
                  print "Please enter choice:"
                  print "1 - menu_option1"
                  print "2 - menu_option2"
                  print "x - Exit"
              
              
              # if i un-tab the below input_choice it goes into an infinite loop
              # but is this way it works just how is want it but does not do the executing
              # of execfile(file_path) at all, which is not what I'm looking at
              
                  input_choice = raw_input( "> " )
              
              
              
              
              
              if input_choice == "1":
                  print("menu_option1")
                  file_path = "C:\\myprog\\menu_option1.py"
                  execfile(file_path)
              
              elif input_choice == "2":
                  print("menu_option2")
                  file_path = "C:\\myprog\\menu_option1.py"
                  execfile(file_path)
              elif input_choice == "x":
                  print("Exit")
                  file_path = "C:\\myprog\\exit.py"
                  execfile(file_path)
              else :
                  print("That is not a valid option. Please re-enter: ")

              Hopefully this should be helpful. Any help would be great!


              Thanks in advance!

              Comment

              • Glenton
                Recognized Expert Contributor
                • Nov 2008
                • 391

                #8
                Hi

                That's what I thought. Again, you can make your code shorter and cleaner here. One thing, is that an infinite loop can be broken out of with the break statement (it's companion is continue, which skips straight to the next cycle in the loop).

                So your code could be done like this (by the way thx for putting the code tags in!):
                Code:
                #set up entry loop
                while True:
                    print "Please enter choice:"
                    print "1 - menu_option1"
                    print "2 - menu_option2"
                    print "x - Exit"
                    input_choice = raw_input( "> " )
                    #check code is valid
                    if len(input_choice)==1:
                        if input_choice in "12x":
                            break    
                    print "invalid entry"
                
                #when you get here, you've got a valid input_choice
                if input_choice == "x":
                    print("Exit")
                    file_path = "C:\\myprog\\exit.py"
                    execfile(file_path)
                else:
                    print("menu_option"+input_choice)
                    file_path = "C:\\myprog\\menu_option%s.py"%input_choice
                    execfile(file_path)
                It's not a huge difference, but will be way easier to add menu items to, and is probably easier to maintain.

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  I'll weigh in for what it's worth:
                  1) what is the best way to create a program monolithic(Like in the Linux kernel all in one) or like the Micro Kernel (All servers communicating with each other to form a single program or Operating system in the case of the Mach kernel)?
                  I don't know how to code like the Linux kernel or an OS, but I would design my program to encapsulate the code and all variables and methods in a series of class objects.

                  2) Are there any real world examples or open source projects based on the above, which I could be directed towards.?
                  BitTorrent was originally written in Python. The source code may still be available.

                  3) Is creating the programs Text based and then converting to GUI a good way to program ?
                  Yes.

                  4) looking a cross-platform GUI based program, is wxpython the right way to go?
                  Tkinter or wxPython would be my choices.

                  5) When going Menu Driven what function to use to call a file within the same folder to the corresponding choice file.
                  Why not import your files instead of executing them? Example:
                  Code:
                  import module1
                  import module2
                  
                  if option == "1":
                      obj = module1.Class1(args)
                  elif option == "2":
                      obj = module2.Class1(args)

                  Comment

                  • Glenton
                    Recognized Expert Contributor
                    • Nov 2008
                    • 391

                    #10
                    I would definitely agree with bvdet that importing and using classes is superior!

                    Comment

                    • d3ph03n1x
                      New Member
                      • Feb 2010
                      • 10

                      #11
                      @Glenton & @bvdet Thank you so much for the help.

                      @bvdet I have found the Bittorrent Source (BitTorrent-5.3-GPL), is it the correct source? or should I be looking for older code ? If so a link would b great. Though if correct, its still going to be very helpful though would be a bit lengthy.

                      When I import the modules is it necessary that the file stays with the same folder where the main file is? its not a compulsion, I was just thinking if its possible.

                      can i have them as below:

                      Code:
                      c:\myprog\data_arranged\menu_option1.py
                      c:\myprog\data_arranged\menu_option2.py
                      c:\myprog\data_arranged\exit.py
                      c:\myprog\main_menu.py

                      Where main_menu.py is the file that is run and menu_option1.py , menu_option.py, exit.py get imported.

                      Thanks once again! :)

                      Comment

                      • Glenton
                        Recognized Expert Contributor
                        • Nov 2008
                        • 391

                        #12
                        Look at these docs, in particular 6.1.2 which talks about the path (where you can put modules that will always be accessible) and 6.4 which talks about packaging modules within subdirectories.

                        Let us know how it goes...

                        Comment

                        • d3ph03n1x
                          New Member
                          • Feb 2010
                          • 10

                          #13
                          Thanks Glenton! Helpful docs. I will stick to the simple importing for the moment. I do have an issue which i'm not sure how to tackle.

                          Code:
                          # define class to add contact
                          class add_cont:
                              def addcontact(self):
                                  fname=raw_input("First Name: ")
                                  lname=raw_input("Last Name: ")
                                  address=raw_input("Address: ")
                                  phone=raw_input("Phone No: ")
                                  mobile=raw_input("Mobile: ")
                          
                          
                                  print("\n\nInfomation Entered: \n")
                          #print the Info just enterd
                                  print("Name: "+fname+" "+lname+"\n"+"Address: "+address+"\n"+"Phone: "+phone+"\n"+"Mobile: "+mobile+"\n")
                          
                          #If correct user saves if not user re-enters data or edits
                                  print("Do you want to save Information?\n")
                          
                          #print("Y(yes) or N(no)\n")
                          
                                  answer=raw_input("Y(yes) or N(no)\n\n:>")
                          where do i check is the answer is yes or know ? And when I try to check within the method it says invalid syntax, secondly either the user would enter "y"||"Y" for yes and "n"||"N" for no. How do i check that as well within the if-else statement. Cause I apply c++ logic and get very confused.

                          Code:
                          #within the menu this is what happens
                          import add_contact
                          contobj=add_cont()
                          
                          #And when user chooses menu_option1(Add contact)
                          
                          #which calls the add contact class and gets the input from the user and verifies
                          #from user and saves or re-inputs data to a file/mysql database 
                          contobj.addcontact()
                          Thanks once again :)

                          Comment

                          • Glenton
                            Recognized Expert Contributor
                            • Nov 2008
                            • 391

                            #14
                            Hi

                            See this post for a method for verification (towards the bottom).

                            I'd be inclined to do this kind of verification *before* passing the data across to the class.

                            Your class is not right, either, BTW. Rather than an "add_contac t" class, make it just a "contact" class. Think nouns not verbs for classes!!

                            Then it needs to be self.whatever = whatever in the __init__ method to change the variable attached to the actual class.

                            Again, telling us what you're trying to achieve is always a better way to get better answers (along with what you've tried, which you gave us).

                            It's probably also better to start a new post for a new idea...

                            Good luck

                            G
                            Last edited by Glenton; Feb 14 '10, 12:27 AM. Reason: typo

                            Comment

                            • Glenton
                              Recognized Expert Contributor
                              • Nov 2008
                              • 391

                              #15
                              BTW, for databases, I often use a dictionary of numpy arrays. Very versatile.

                              Comment

                              Working...