Dr.Python: running same output again

Reply

Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Dr.Python: running same output again

 
0
  #1
Jan 9th, 2007
Is there an option of some sort in the Dr. Python interpreter that would allow one to re-run the same output results after making adjustments to the code.

Example:
I run an example of source code and receive an output result. I make changes to the code. I now am able to run the same code (with the same output) to see how the new code affects a particular output result.

This just came to mind today while doing testing on a personal Python project. This would really save time and allow me to see reactions to adjusted code immediatly rather than hoping to come upon the same bug by running code again and again manually.

I am sure something like this exists in compilers in general, I imagine, but I have never made use of it.

Thanks for any help in advance.

sharky_machine
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,523
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 169
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Dr.Python: running same output again

 
0
  #2
Jan 10th, 2007
Sharky,

DrPython is not a compiler nor an interpreter, it is simply an fancy editor that allows you to run code from.

I assume from your question that you want to retain the previous outputs in your output window, so you can compare them?
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Dr.Python: running same output again

 
0
  #3
Jan 14th, 2007
Do you mean some kind of automated testing, where you can pass the same input to the code each time?

There are ways of doing that that are really sophisticated, but I usually just wrap the code in some kind of test() function that tests each class or function.

Writing the test() is a bear, but then you can just run the file, type test(), and relax. I mean, go and fix the bugs. At least in my case.
:lol:
Jeff
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,972
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 920
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Dr.Python: running same output again

 
0
  #4
Jan 14th, 2007
You could write a small Python program that will run your script repeatedly (put a time delay into the loop) with arguments you give it ...
  1. # run an external program from within Python code with
  2. # subprocess.call(["program-name", "arg1", "arg2", ...])
  3. import subprocess
  4.  
  5. # part of a loop
  6. subprocess.call(["C:/Python25/Python.exe", "MyScript.py", "arg1", "arg2"])
Then write your MyScript.py in such a way that the output goes to a file with a time/date stamp in the filename. I think there is a suggestion how to do this in the "Starting Python" sticky. Finally compare the files with another Python program.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,972
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 920
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Dr.Python: running same output again

 
0
  #5
Jan 20th, 2007
Matty,
I was thinking about your question. Python has a module to do automatic unit testing, appropriately called 'unittest'. Take a look in the Python manual, there are examples.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Dr.Python: running same output again

 
0
  #6
Jan 21st, 2007
Intrigued, I coded up a simple unittest example.

  1. import unittest
  2.  
  3. # The class to test
  4. class Rectangle(object):
  5.  
  6. def __init__(self, w, h):
  7. self.width = w
  8. self.height = h
  9.  
  10. def get_width(self):
  11. return self._w
  12.  
  13. def set_width(self, w):
  14. if w == None:
  15. raise ValueError
  16. self._w = w
  17.  
  18. width = property(get_width, set_width)
  19.  
  20. def get_height(self):
  21. return self._h
  22.  
  23. def set_height(self, h):
  24. if h == None:
  25. raise ValueError
  26. self._h = h
  27.  
  28. height = property(get_height, set_height)
  29.  
  30. def area(self):
  31. return self.height * self.width
  32.  
  33.  
  34. class TestRectangle(unittest.TestCase): # the magic occurs here
  35. # by subclassing TestCase
  36.  
  37. def setUp(self):
  38. self.test_cases = [(2,3),(0,5),(-2.5,6)]
  39. self.test_widths = [2,0,-2.5]
  40. self.test_heights = [3,5,6]
  41. self.test_areas = [6,0,-15]
  42. # the last element could be the subject of debate:
  43. # Should rectangles be allowed to have signed area and sides?
  44. # In the interest of physics, I decided Yes' (think flux here), but
  45. # here in the test suite is the place that debate amongst the
  46. # programming team could occur.
  47.  
  48. self.error_cases = [(None,3),(5,None),(None,None)]
  49. self.error_results = [ValueError,ValueError,ValueError]
  50.  
  51. def testHeight(self):
  52.  
  53. self.Rects = [Rectangle(*x) for x in self.test_cases]
  54. self.assertEqual([x.height for x in self.Rects], self.test_heights)
  55.  
  56. def testWidth(self):
  57.  
  58. self.Rects = [Rectangle(*x) for x in self.test_cases]
  59. self.assertEqual([x.width for x in self.Rects], self.test_widths)
  60.  
  61. def testArea(self):
  62.  
  63. self.Rects = [Rectangle(*x) for x in self.test_cases]
  64. self.assertEqual([x.area() for x in self.Rects], self.test_areas)
  65.  
  66. def testErrors(self):
  67.  
  68. errors = dict(zip(self.error_cases, self.error_results))
  69. for x in errors:
  70. w,h = x
  71. self.assertRaises(errors[x], Rectangle, w, h)
  72.  
  73. if __name__ == "__main__":
  74.  
  75. unittest.main() #automagically creates an instance of my
  76. # TestRectangle class, and runs each method.

Here's the output with all Rectangle methods broken:

  1. FFEE
  2. ======================================================================
  3. ERROR: testHeight (__main__.TestRectangle)
  4. ----------------------------------------------------------------------
  5. Traceback (most recent call last):
  6. File "C:/Python24/unittest_test.py", line 50, in testHeight
  7. self.assertEqual([x.height for x in self.Rects], self.test_heights)
  8. File "C:/Python24/unittest_test.py", line 21, in get_height
  9. return self._h
  10. AttributeError: 'Rectangle' object has no attribute '_h'
  11.  
  12. ======================================================================
  13. ERROR: testWidth (__main__.TestRectangle)
  14. ----------------------------------------------------------------------
  15. Traceback (most recent call last):
  16. File "C:/Python24/unittest_test.py", line 55, in testWidth
  17. self.assertEqual([x.width for x in self.Rects], self.test_widths)
  18. File "C:/Python24/unittest_test.py", line 10, in get_width
  19. return self._w
  20. AttributeError: 'Rectangle' object has no attribute '_w'
  21.  
  22. ======================================================================
  23. FAIL: testArea (__main__.TestRectangle)
  24. ----------------------------------------------------------------------
  25. Traceback (most recent call last):
  26. File "C:/Python24/unittest_test.py", line 60, in testArea
  27. self.assertEqual([x.area() for x in self.Rects], self.test_areas)
  28. AssertionError: [None, None, None] != [6, 0, -15]
  29.  
  30. ======================================================================
  31. FAIL: testErrors (__main__.TestRectangle)
  32. ----------------------------------------------------------------------
  33. Traceback (most recent call last):
  34. File "C:/Python24/unittest_test.py", line 67, in testErrors
  35. self.assertRaises(errors[x], Rectangle, w, h)
  36. AssertionError: ValueError not raised
  37.  
  38. ----------------------------------------------------------------------
  39. Ran 4 tests in 0.015s
  40.  
  41. FAILED (failures=2, errors=2)
  42.  
  43. Traceback (most recent call last):
  44. File "C:/Python24/unittest_test.py", line 71, in -toplevel-
  45. ut.main()
  46. File "C:\Python24\lib\unittest.py", line 759, in __init__
  47. self.runTests()
  48. File "C:\Python24\lib\unittest.py", line 797, in runTests
  49. sys.exit(not result.wasSuccessful())
  50. SystemExit: True
and with all Rectangle methods set to their correct code:

  1. ....
  2. ----------------------------------------------------------------------
  3. Ran 4 tests in 0.015s
  4.  
  5. OK
  6.  
  7. Traceback (most recent call last):
  8. File "C:/Python24/unittest_test.py", line 68, in -toplevel-
  9. unittest.main()
  10. File "C:\Python24\lib\unittest.py", line 759, in __init__
  11. self.runTests()
  12. File "C:\Python24\lib\unittest.py", line 797, in runTests
  13. sys.exit(not result.wasSuccessful())
  14. SystemExit: False
My question: is that last bit normal? Why the complicated 'sys.exit(not result.wasSuccessful())'?

Anyways, the OK is the sign that all is well with the world.

Jeff
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,972
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 920
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Dr.Python: running same output again

 
0
  #7
Jan 21st, 2007
Jeff, I tested your code sample and did not get the goofy traceback. However, I streamlined the indent on some of your comment lines.

I am using Python24 on a Windows XP machine.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC