Opening an exe

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • defience
    New Member
    • Jan 2007
    • 27

    Opening an exe

    I need to write a program that will open an exe and check some timings in it.

    point to exe
    run exe
    check time taken between numbers:
    123 (08 secs)
    341 (1 sec)
    444 (09 secs)

    What python functions do I need to research to accomplish this? Thanks
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by defience
    I need to write a program that will open an exe and check some timings in it.

    point to exe
    run exe
    check time taken between numbers:
    123 (08 secs)
    341 (1 sec)
    444 (09 secs)

    What python functions do I need to research to accomplish this? Thanks
    I use _winreg to find out the path of the exe:
    Code:
            try:
                ### Read registry for InstaCal path
                instacalKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
                                            "SOFTWARE\Universal Library")
                self.pmdPath = str(_winreg.QueryValueEx(instacalKey, 'RootDirs')[0])
                instacalKey.Close()
            except WindowsError:
                self.InstaCalBtn.Disable()
                self.pmdPath = ""
    then:
    Code:
        def OnInstaCalButton(self, event):
            os.spawnl(os.P_DETACH, self.pmdPath + "inscal32.exe")
    Need more info on the time between numbers, but you might want to check out the time module.

    Comment

    • defience
      New Member
      • Jan 2007
      • 27

      #3
      Thank you for the reply. I know the path of the exe. Could I do something like this:

      Code:
      import os, sys
      
      pyPath = "C:\python25\app.exe"
      As far as the numbers go, the script will need 4 integer variables from 0-1000 and will slow when the right number is crossed and then move on to another series of 0-1000, and continue to loop for a total of 4 times.
      Here is a sample C++ code snippet that might help.
      ex: system("app.exe 1 1 1 1") was used to call the application.

      Code:
      MaxR = -1.0;
      for(i=1; i<1000; i++){
               ... code
          
                system(sRun);
      
          ....code
      
          R = ... the elapsed time of the function system().
          if (R > MaxR){
              MaxR = R;
              MaxI = i;
          }
      }
      
      printf ("Max(i) = %d  elapsed %.9f  secs \n", MaxI, MaxR);
      ex: system("app.exe 1 1 1 1") was used to call the application. Does python have something similar?

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by defience
        Thank you for the reply. I know the path of the exe. Could I do something like this:

        Code:
        import os, sys
        
        pyPath = "C:\python25\app.exe"
        You must use raw strings or unicode strings for windows path names. Otherwise you get "escape" sequences. ie "C:\test" is actually "C:[tab]est".
        Code:
        import os, sys
        appPath = r"C:\python25\app.exe"
        # or
        appPath = u"C:\python25\app.exe"
        I prefer raw strings. Boa Construtor uses unicode strings.

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by defience
          As far as the numbers go, the script will need 4 integer variables from 0-1000 and will slow when the right number is crossed and then move on to another series of 0-1000, and continue to loop for a total of 4 times.
          Here is a sample C++ code snippet that might help.
          ex: system("app.exe 1 1 1 1") was used to call the application.

          Code:
          MaxR = -1.0;
          for(i=1; i<1000; i++){
                   ... code
              
                    system(sRun);
          
              ....code
          
              R = ... the elapsed time of the function system().
              if (R > MaxR){
                  MaxR = R;
                  MaxI = i;
              }
          }
          
          printf ("Max(i) = %d  elapsed %.9f  secs \n", MaxI, MaxR);
          ex: system("app.exe 1 1 1 1") was used to call the application. Does python have something similar?
          There are a few ways to do this:
          Code:
          from time import time
          startTime = time()
          for i in xrange(1000):
              # simulate an external process
              for i in range(10000):
                  pass
          
          print "Process took %f seconds" % (time() - startTime)

          There is a discussion on the timeit module here.

          Comment

          • defience
            New Member
            • Jan 2007
            • 27

            #6
            Originally posted by bartonc
            There are a few ways to do this:
            Code:
            from time import time
            startTime = time()
            for i in xrange(1000):
                # simulate an external process
                for i in range(10000):
                    pass
            
            print "Process took %f seconds" % (time() - startTime)

            There is a discussion on the timeit module here.
            Awesome! That's a great start for me. Thank you! I see what you mean about the raw string as I was getting an error message with app.exe.

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by defience
              Awesome! That's a great start for me. Thank you! I see what you mean about the raw string as I was getting an error message with app.exe.
              Go get 'me and have fun! Keep posting,
              Barton

              Comment

              • defience
                New Member
                • Jan 2007
                • 27

                #8
                Ok, so I found a couple of different ways to get python to run the app.exe, but how do I incorporate the timing code to the exe? If I use the following code, it will run, regardless of the app.exe.
                Code:
                Code:
                from time import time
                startTime = time()
                for i in xrange(1000):
                       # simulate an external process
                for i in range(1000):
                     pass
                
                print "Process took %f seconds" % (time() - startTime)
                This will run the app.exe
                Code:
                Code:
                import subprocess
                
                subprocess.Popen(r"C:\python25\app.exe")
                or this one, which will run the timer and then the app.exe. It sometimes gives me a NameError: name 'MZP' is not defined, but I have no idea where that comes from since there is no 'MZP' in the code.
                Code:
                Code:
                import os,sys
                
                pyPath=r"C:\python25\python.exe"
                appPath=r"C:\python25\app.exe"
                os.spawnv(os.P_NOWAIT,pyPath,["python",appPath])
                
                from time import time
                startTime = time()
                for i in xrange(1000):
                    # simulate an external process
                    for i in range(10000):
                        pass
                
                print "Process took %f seconds" % (time() - startTime)

                Comment

                • bartonc
                  Recognized Expert Expert
                  • Sep 2006
                  • 6478

                  #9
                  Originally posted by defience
                  Ok, so I found a couple of different ways to get python to run the app.exe, but how do I incorporate the timing code to the exe? If I use the following code, it will run, regardless of the app.exe.
                  In the 2.4 Python Manuals the section is "6.1.5 Process Management" of the os module documentation. It describes how you may be able to wait for an exit code from the external process.

                  Comment

                  • dshimer
                    Recognized Expert New Member
                    • Dec 2006
                    • 136

                    #10
                    Code:
                    import os,sys
                    
                    pyPath=r"C:\python25\python.exe"
                    appPath=r"C:\python25\app.exe"
                    os.spawnv(os.P_NOWAIT,pyPath,["python",appPath])
                    This seems to me that it would be on the right track except it looks like it would evaluate out to a command line similar to.

                    C:\python25\pyt hon.exe C:\python25\app .exe

                    which is just trying to call an executable in python itself or am I missing something? As I play with the spawnv, I would think that you would just want to set a start time, run something like

                    os.spawnv(os.P_ NOWAIT,appPath,["appname",AnyAr gsForApp])

                    then check the end time.

                    Comment

                    • bartonc
                      Recognized Expert Expert
                      • Sep 2006
                      • 6478

                      #11
                      Originally posted by dshimer
                      Code:
                      import os,sys
                      
                      pyPath=r"C:\python25\python.exe"
                      appPath=r"C:\python25\app.exe"
                      os.spawnv(os.P_NOWAIT,pyPath,["python",appPath])
                      This seems to me that it would be on the right track except it looks like it would evaluate out to a command line similar to.

                      C:\python25\pyt hon.exe C:\python25\app .exe

                      which is just trying to call an executable in python itself or am I missing something? As I play with the spawnv, I would think that you would just want to set a start time, run something like

                      os.spawnv(os.P_ NOWAIT,appPath,["appname",AnyAr gsForApp])

                      then check the end time.
                      Yep. It is the right track. It's the "os.P_NOWAI T" that is in question.

                      Comment

                      • dshimer
                        Recognized Expert New Member
                        • Dec 2006
                        • 136

                        #12
                        Should have put this in before, but for example to run the find command on my /tmp directory and time it
                        Code:
                        >>> def test():
                        ... 	start=time.time()
                        ... 	os.spawnv(os.P_WAIT,'c:/unix/find.exe',['find','/tmp'])
                        ... 	print time.time()-start
                        ... 
                        >>> test()
                        0.950999975204

                        Comment

                        • dshimer
                          Recognized Expert New Member
                          • Dec 2006
                          • 136

                          #13
                          Originally posted by bartonc
                          Yep. It is the right track. It's the "os.P_NOWAI T" that is in question.
                          Good call, I didn't even catch that in the original post, I just went by the docs which is why the example works, but the answer was incomplete.

                          Comment

                          • bartonc
                            Recognized Expert Expert
                            • Sep 2006
                            • 6478

                            #14
                            Originally posted by dshimer
                            Good call, I didn't even catch that in the original post, I just went by the docs which is why the example works, but the answer was incomplete.
                            Great posting, D. I really appreciate all of your input. Thank you,
                            Barton

                            Comment

                            • defience
                              New Member
                              • Jan 2007
                              • 27

                              #15
                              Thank you both for your replies! One more thing.....how could I do this:
                              Code:
                              i = 0
                              for i in range(0,30):
                                   print i
                                   i +=1
                              ....have the double digits print out with a space between them:
                              2 0
                              2 1
                              2 2 etc...

                              Would something like this work? i = 0 0?

                              Comment

                              Working...