473,320 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to avoid using files to store intermediate results

I wrote a small wxPython based app to test code snippets.
(google for "python lightning compiler" if you want to see the full
code).

In the basic mode, I redirect the standard input and output and
execute the code taken from the editor window so that the result
appears in the output window.

Here are the 4 main lines of code to accomplish this:

sys.stdout = self.outputWindow
sys.stderr = self.outputWindow
user_code = self.PythonEditor.GetText()
exec user_code in myGlobals

For example, if I have the following situation:
====input window====
print "Hello world!"
====================

the result of running the program will be this:
====output window===
Hello world!
====================

Simple enough :-) [Actually, there's more to it, but this description
should suffice for illustration purposes.]
Now, I want to be able to test the code using the doctest module.

I can't use exec as doctest.testmod() will be testing my entire
application, not simply the code in the input window!

The solution I chose was to
1. Create a new file which contains the code to be tested with the
appropriate "doctest" call.
2. Run this file with Python, redirecting the result to a second file.
3. Read the result from the second file into a string.
4. Print the string (which, because of redirection) appears in the
output window.

Here are the main lines of code to do this:

sys.stdout = self.outputWindow
sys.stderr = self.outputWindow
user_code = self.PythonEditor.GetText()

user_code += "\nimport doctest\ndoctest.testmod()"

f = open('_doctest_file.py', 'w')
f.write(user_code)
f.close()

if verbose:
os.popen("python _doctest_file.py -v> _doctest_file.output")
else:
os.popen("python _doctest_file.py> _doctest_file.output")
result = open("_doctest_file.output", 'r').read()
print result

######
While this works, I find it "messy", as it creates some intermediate
files. I was wondering if there was a better way to do things all in
memory, in an OS independent way.

[Note that the complete application is approximately 665 lines long
.... a bit too much to post all here :-)]

André

Apr 26 '06 #1
1 1224
André wrote:
Now, I want to be able to test the code using the doctest module.

I can't use exec as doctest.testmod() will be testing my entire
application, not simply the code in the input window! While this works, I find it "messy", as it creates some intermediate
files.**I*was*wondering*if*there*was*a*better*way* to*do*things*all*in
memory, in an OS independent way.


Here's a manual setup you might be able to adapt:

import doctest
from cStringIO import StringIO

sample = """
print "hello" hello "hello"

'hello'
"""

globs = {}
out = StringIO()
parser = doctest.DocTestParser()
test = parser.get_doctest(sample, globs, "<noname>", "<nofile>", 0)
runner = doctest.DocTestRunner(verbose=True)
runner.run(test, out=out.write)
print out.getvalue()

Peter
Apr 26 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Maurice LING | last post by:
Hi, I know that PLY lex is able to do line counting. I am wondering if there is a way to count the number of each keywords (tokens) in a given file? For example, how many IF tokens etc? ...
2
by: Sam | last post by:
I would like to store html templates in a database. By using perl I would like to retrive the template ask the user to fill the template and store the whole file is template + the user data in a...
0
by: Matthias Martetschlaeger | last post by:
Hi everyone, we use Clearcase dynamic Views to get our Source-Files. Due speed reasons, the output files should not go to the same location (dynamic view) as the source files are located, this...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
2
by: Deano | last post by:
Hi, I have created a new form that reads in data from some intermediate tables. These intermediate tables are used to compile data for use in a report or the new form I have made. Having...
4
by: | last post by:
Hello, I have a client that is feeding PDF files to me. They do not want me to link to their files since that can put a strain on their bandwidth. What they asked I do is download the file and...
5
by: rohitkumar | last post by:
I am trying to build a web application which can execute dos print command on the client machine. This application will read a file stored in server and it will create a folder on client machine...
36
by: CK | last post by:
How do I write a set based query? I have a groupSets table with fields setId, idField, datasource, nameField, prefix, active Data: 1,someIDfield, someTable, someField, pre1, 1 2,someotherIDfield,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.