[visionegg] Re: how to output the result to a file
- From: "Neil Halelamien" <neilh@xxxxxxxxxxx>
- To: visionegg@xxxxxxxxxxxxx
- Date: Wed, 21 Nov 2007 17:33:26 -0800
Speaking of data output, I've attached some code I wrote in the past
for logging data in a human-readable & machine-readable format, where
you can supply the names of variables in the current space that you'd
like to write to file. It uses what I think is a pretty nasty hack
that probably isn't guaranteed to work in future versions of Python,
but for the time being it's been useful to me.
On Nov 21, 2007 5:20 PM, Eamon Caddigan <ecaddiga@xxxxxxxx> wrote:
> Here's a good tutorial on writing things to files (part of a larger
> good tutorial on python): http://www.diveintopython.org/file_handling/
> file_objects.html#d0e15055
>
> Note that writing to (and reading from) disk is a relatively slow
> operation for a computer. It does its best by buffering output, but
> if your experiment is sensitive to timing issues, it's typically best
> to save data in another structure (e.g., a list of coordinate
> tuples), and only write that info to a file at the end of your trials.
>
> HTH,
> Eamon
>
>
>
> On Nov 21, 2007, at 7:01 PM, Lili Wu wrote:
>
> > Hi,
> > I have a question about how to output the running result of a program.
> > Take the demo of mouseTarget.py for example, I can use "print x,y "
> > to output every mouseposition in the Python Shell window. Is there
> > any method to output this data to a file, like a *.txt file?
> >
> > Thanks!
> >
> > Lili
> >
> > 雅虎邮箱,终生伙伴!
>
> The Vision Egg mailing list
> Archives: http://www.freelists.org/archives/visionegg
> Website: http://www.visionegg.org/mailinglist.html
>
>
# written by Neil Halelamien
#import inspect
import time
import os
import sys
class DataLogger:
# directory: the directory to save the data file in
# filetag: an id tag, such as the experiment type and subject ID
def __init__(self, datadir, filetag):
#begintime_tag = time.strftime("%Y-%m-%d_%H.%M.%S", time.localtime())
file_fragment = filetag + '.txt'
self.filename = os.path.join(datadir, file_fragment)
os.makedirs(os.path.join(datadir, filetag)) # also create directory
with same name as datafile
self.file = open(self.filename, 'w')
# Writes the variables with the specified names out to the file
# Uses some hacks to access variables from up the stack
# NOTE: right now doesn't work on member variables, e.g. foo.bar
def writevars(self, *args):
f = sys._getframe(1)
#print f.f_locals
for varname in args:
assert type(varname) is str
vardata = f.f_locals[varname]
dataline = varname + ' ' + repr(vardata)
print dataline
self.file.write(dataline + '\n')
# Should be called when done with DataLogger
def close(self):
self.file.close()
- Follow-Ups:
- [visionegg] Re: how to output the result to a file
- From: Mark Halko
- References:
- [visionegg] how to output the result to a file
- From: Lili Wu
- [visionegg] Re: how to output the result to a file
- From: Eamon Caddigan
Other related posts:
- » [visionegg] how to output the result to a file
- » [visionegg] Re: how to output the result to a file
- » [visionegg] Re: how to output the result to a file
- » [visionegg] Re: how to output the result to a file
- [visionegg] Re: how to output the result to a file
- From: Mark Halko
- [visionegg] how to output the result to a file
- From: Lili Wu
- [visionegg] Re: how to output the result to a file
- From: Eamon Caddigan