View Single Post
Old 01-22-2011, 06:32 AM   #64
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,486
Karma: 8025704
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by pchrist7 View Post
Ahh - never thought of that.
Will try this.
Any way to "set -x" like in most unix shells ?
First, thank you for taking the lead on getting a user-friendly version of this set up!

Now to your question: What are you trying to do? When you run a .bat file in a windows box, you get '-x'-like behavior for the commands in the file. However, the commands are extremely uninteresting, so this can't be what you are thinking of.

If you are thinking of a python execution trace, then yes, but it is somewhat complex. The following example shows one way to do it. What you want in 'evaluate' goes into 'myfunc', and 'evaluate' becomes the controller that sets up the trace. This example runs for me. Note that when looking at the output, the line number of the myfunc line is 4, not 1, because there is stuff you are not seeing.

Code:
def myfunc(self, formatter, kwargs, mi, locals, val, is_read_pct, is_read_str, 
				is_not_read_str, no_page_read_str):
	try:
		test_val = int(is_read_pct)
	except:
		return 'is_read_pct is not a number'
	print 'here'
	import re
	mg = re.match('.*last page read: location \d+ \((\d+)%\)', val, re.I + re.DOTALL);
	if mg is None:
		return no_page_read_str
	print 'here2'
	try:
		f = int(mg.group(1))
		if f >= test_val:
			return is_read_str
		return is_not_read_str
	except:
		pass
	return no_page_read_str

def evaluate(self, *args):

	import linecache, inspect, sys

	def traceit(frame, event, arg):
		if event == 'line':
			lineno = frame.f_lineno
			if '__file__' in frame.f_globals:
				filename = frame.f_globals['__file__']
				if (filename.endswith('.pyc') or
					filename.endswith('.pyo')):
					filename = filename[:-1]
				name = frame.f_globals['__name__']
				line = linecache.getline(filename, lineno)
			else:
				name = '[unknown]'
				try:
					src = inspect.getsourcelines(frame)
					line = src[lineno]
				except IOError:
					line = 'Unknown code named [%s].  VM instruction #%d' % \
						(frame.f_code.co_name, frame.f_lasti)
			print '%s:%s: %s' % (name, lineno, line.rstrip())
		return traceit

	sys.settrace(traceit)
	try:
		rv = self.myfunc(*args)
	except:
		rv = 'EXCEPTION'
	sys.settrace(None)
	return rv
Acknowledgment: code for the traceit function from http://www.friday.com/bbum/2005/10/2...hon-execution/
chaley is offline   Reply With Quote