View Single Post
Old 01-22-2011, 07:17 AM   #65
pchrist7
Addict
pchrist7 is kind to children and small, furry animalspchrist7 is kind to children and small, furry animalspchrist7 is kind to children and small, furry animalspchrist7 is kind to children and small, furry animalspchrist7 is kind to children and small, furry animalspchrist7 is kind to children and small, furry animalspchrist7 is kind to children and small, furry animalspchrist7 is kind to children and small, furry animalspchrist7 is kind to children and small, furry animalspchrist7 is kind to children and small, furry animalspchrist7 is kind to children and small, furry animals
 
pchrist7's Avatar
 
Posts: 385
Karma: 6514
Join Date: Aug 2010
Location: Denmark
Device: Kindle 3 3G+Wifi, Oasis
Quote:
Originally Posted by chaley View Post
First, thank you for taking the lead on getting a user-friendly version of this set up!
Well, I DID promise to do it, right ?

Quote:
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.
In a unix shell script you use #!/usr/bin/ksh -x as first line in script, or any where else in script use set -x
Then you'll see, line by line, how the scripts is executed, what value is assigned to variables, comparisons, etc.
I was more looking for a "python -x " thingy that could do something similar.
Forgive my ignorance, but sometimes wishes DO come true. ( I heard )

Quote:
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.
Thanks. Will give it a try sometime.
Still working, but now from home


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/[/QUOTE]

Will give it a couple of test runs tomorrow
BTW: You have any tips for my latest added "edit" comments ?
Hoping beckywc will try my howto during the weekend.
Did 3 test runs, in 3 different Calibre libs.
My main lib with 1k books works fine, no performance penalty I can detect.
Have a nice weekend

Edit: I forgot to mention that I really like your thinking :
return is_reading_str + ': ' + pct + '%'

Thats is NEAT !
Never thought of that myself.
Let's see if bekywc and others choke, or love it :-)

Last edited by pchrist7; 01-22-2011 at 07:25 AM. Reason: forgot
pchrist7 is offline   Reply With Quote