User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 397,809 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,524 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Views: 1489 | Replies: 6
Reply
Join Date: Oct 2006
Location: New York City
Posts: 2,553
Reputation: mattyd is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Dr.Python: running same output again

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 1,020
Reputation: Ene Uran is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 64
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Veteran Poster

Re: Dr.Python: running same output again

  #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  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: Dr.Python: running same output again

  #3  
Jan 13th, 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  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,414
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: Dr.Python: running same output again

  #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  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,414
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: Dr.Python: running same output again

  #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  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: Dr.Python: running same output again

  #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:

FFEE
======================================================================
ERROR: testHeight (__main__.TestRectangle)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:/Python24/unittest_test.py", line 50, in testHeight
    self.assertEqual([x.height for x in self.Rects], self.test_heights)
  File "C:/Python24/unittest_test.py", line 21, in get_height
    return self._h
AttributeError: 'Rectangle' object has no attribute '_h'

======================================================================
ERROR: testWidth (__main__.TestRectangle)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:/Python24/unittest_test.py", line 55, in testWidth
    self.assertEqual([x.width for x in self.Rects], self.test_widths)
  File "C:/Python24/unittest_test.py", line 10, in get_width
    return self._w
AttributeError: 'Rectangle' object has no attribute '_w'

======================================================================
FAIL: testArea (__main__.TestRectangle)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:/Python24/unittest_test.py", line 60, in testArea
    self.assertEqual([x.area() for x in self.Rects], self.test_areas)
AssertionError: [None, None, None] != [6, 0, -15]

======================================================================
FAIL: testErrors (__main__.TestRectangle)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:/Python24/unittest_test.py", line 67, in testErrors
    self.assertRaises(errors[x], Rectangle, w, h)
AssertionError: ValueError not raised

----------------------------------------------------------------------
Ran 4 tests in 0.015s

FAILED (failures=2, errors=2)

Traceback (most recent call last):
  File "C:/Python24/unittest_test.py", line 71, in -toplevel-
    ut.main()
  File "C:\Python24\lib\unittest.py", line 759, in __init__
    self.runTests()
  File "C:\Python24\lib\unittest.py", line 797, in runTests
    sys.exit(not result.wasSuccessful())
SystemExit: True

and with all Rectangle methods set to their correct code:

....
----------------------------------------------------------------------
Ran 4 tests in 0.015s

OK

Traceback (most recent call last):
  File "C:/Python24/unittest_test.py", line 68, in -toplevel-
    unittest.main()
  File "C:\Python24\lib\unittest.py", line 759, in __init__
    self.runTests()
  File "C:\Python24\lib\unittest.py", line 797, in runTests
    sys.exit(not result.wasSuccessful())
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  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,414
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: Dr.Python: running same output again

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Python Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 6:22 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC