Re: [Tutor] Do not understand why I am getting "EOFError: EOF when reading a line".
Hi On 8 October 2016 at 05:51, boB Steppwrote: > I think I now understand why I am getting this EOF exception. > > On Fri, Oct 7, 2016 at 10:16 PM, boB Stepp wrote: >> My current get_input() function: >> >> == >> def get_input(): >> '''Get string input from the user and convert it to an integer. This >> integer is returned to the caller. >> >> :num_sides: Number of sides for the displayed grid.''' >> >> while True: >> try: >> num_sides = int(input("Number of sides = ")) >> return num_sides >> except ValueError: >> print("'Number of sides' must be a positive integer. Please >> enter " >> "an integer.") >> === > > Since the try/except is within a while loop, the input tries to read > more input after the ValueError occurs. Of course, I don't have any > more input strings stored in the StringIO buffer, so it sees this as > an end of file condition. > > But now I do not see how to test get_input() to check for the > ValueError. Any suggestions? Your test is written such that it checks/expect ValueError to be thrown from get_input(). However, you're in fact catching/trapping ValueError inside get_input() so it's never going to be seen by the test. You cannot have it both ways. Either your intention is that get_input() must raise ValueError as your test expects it (and therefore get_input() shouldn't be [responsible for] catching, printing and looping as it currently is), or it must not (and should keep trying until it successfully reads an int). If the latter is the case your test needs to change to reflect this reality (by e.g. perhaps altering what's in the StringIO() input for example, so that there is a valid in in the input in addition to the invalid 'a', to allow the loop to terminate. Then the test should simply check that get_input() successfully "ignores"/skips over the invalid input and retrieves that latter integer from the test "input stream" [e.g. what you put into the StringIO()] and not expect an exception to be raised.) Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Python Shell
Hi. I have just installed Python 2.7.12 ( i have windows xp). I created a program called HW.py that runs under a command prompt, but Python Shell gives the following error: Traceback (most recent call last): File "", line 1, in HW.py NameError: name 'HW' is not defined Can someone please tell me what I'm doing wrong? I have added the Python directory to the Path. Alan. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Accessing Yahoo with Python?
I realize that my question is not about the standard library. The only reason I am asking here is, if I remember correctly, a regular contributor, Danny Yoo, works at Yahoo. I am hoping he, or someone else here can help me understand what is happening. I am writing a program using the yahoo_finance module. Over the past week I have been testing and refining it. At first the only errors I was seeing were mine. Lately I have been a lot of these errors. Traceback (most recent call last): File "/usr/local/lib/python3.4/dist-packages/yahoo_finance/__init__.py", line 120, in _request _, results = response['query']['results'].popitem() KeyError: 'query' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/jfb/MyProgs/StockHistory/stock_history_oop.py", line 75, in Stocks().run() File "/home/jfb/MyProgs/StockHistory/stock_history_oop.py", line 38, in run self.get_stock_info() File "/home/jfb/MyProgs/StockHistory/stock_history_oop.py", line 53, in get_stock_info yahoo = Share(stk) File "/usr/local/lib/python3.4/dist-packages/yahoo_finance/__init__.py", line 178, in __init__ self.refresh() File "/usr/local/lib/python3.4/dist-packages/yahoo_finance/__init__.py", line 142, in refresh self.data_set = self._fetch() File "/usr/local/lib/python3.4/dist-packages/yahoo_finance/__init__.py", line 181, in _fetch data = super(Share, self)._fetch() File "/usr/local/lib/python3.4/dist-packages/yahoo_finance/__init__.py", line 134, in _fetch data = self._request(query) File "/usr/local/lib/python3.4/dist-packages/yahoo_finance/__init__.py", line 123, in _request raise YQLQueryError(response['error']['description']) yahoo_finance.YQLQueryError: Query failed with error: "'No definition found for Table yahoo.finance.quotes'". It has gotten to the point where if I run the program 10 times, 9 of them will return the above error before I get the data I want. The part of my program that retrieves the data from Yahoo has not changed in quite a while. Is it possible that I have exceeded a Yahoo limit on requests from one IP address? Test data I am using gets the historical info on 3 stocks over a 3 day period. Thanks, Jim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] TDD: How to test input()?
On 08/10/16 07:18, Steven D'Aprano wrote: > Testing code that depends on user input is always tricky. There are a > few ways to deal with it: > > (5) Or you could manually monkey-patch sys.stdin as you do. A variation on this that I have used is to read the input from stdin as usual but redirect stdin when you run the code: python test_input.py < testdata.txt That way I can use multiple sets of test data and run the tests using the different data sets. The test suite is then exercised by a simple shell script that runs against each test file in turn. I find that easier and more flexible than using a StringIO buffer. You can catch EOF errors in your test rig or write the tests to read a finite set of data and create the data to match. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] TDD: How to test input()?
On Fri, Oct 07, 2016 at 08:26:28PM -0500, boB Stepp wrote: > I think I have this figured out, but I want to be certain I am doing > it both correctly and in the preferred way. I have a need for a > get_input() function and have written my first test for this function > as follows: Testing code that depends on user input is always tricky. There are a few ways to deal with it: (1) Denial. Just don't test that part of your code at all. That's not ideal, but it may be "good enough" if any error in that code would be obvious enough. (2) Perform ad hoc non-automated tests that require user intavention. That's a bit better but still not great. (3) You can monkey-patch the input() function in your test: import mymodule # module being tested class MyTests(unittest.TestCase): def test_get_input(self): def myinput(prompt): return "5" mymodule.input = myinput try: result = mymodule.get_input("What number do you want?") self.assertEqual(result, 5) finally: del mymodule.input That's often a good solution for simple testing. (4) Python 3 unittest library now includes support for mocking: https://docs.python.org/3/library/unittest.mock.html which lets you replace parts of your code with fake code ("mocks") which are more easily tested, similar to the monkey-patch example in (3) above. For an example, look at the test suite for the "code" module: https://docs.python.org/3/library/code.html https://hg.python.org/cpython/file/3.5/Lib/test/test_code_module.py (5) Or you could manually monkey-patch sys.stdin as you do. I haven't studied your code in detail to comment on it yet. Time permitting, I shall do so later. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor