#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    80
    Rep Power
    2

    Showing Off the Clocker Script


    So, remember all that time I spent working on something with timeit?

    I think that might have paid off.

    Test script-
    Code:
    from pygos.clocker import Clocker
    
    def allforall(*args):
    	args=list(args)
    	placeholder=args.pop(0)
    	frame=list(placeholder) #'frame' and 'placeholder' start as copies of each other. This changes, however.
    	while len(args)>0:
    		placeholder=list(frame)
    		frame,next_frame=list(),args.pop(0)
    		for item1 in placeholder:
    			for item2 in next_frame:
    				frame.append(item1+item2)
    	return frame
    
    
    stopwatch=Clocker()
    stopwatch.fetch=[allforall]
    stopwatch.setup={'iter1':['red','blue','green'],'iter2':['cat','dog','monkey']}
    stopwatch.tests=['allforall(iter1,iter2)']
    In prompt...
    Code:
    >>> stopwatch.run()
    {'allforall(iter1,iter2)': 0.00015895875034396828}

    Using inspect, it figures out what everything is and from whence it is imported. Just throw it in "fetch" and it'll track down the source and make sure it's imported into the setup script.

    Clocker.fetch-A list of functions, classes, or objects to track down from whatever modules they were in in the current session. On Clocker.make_script(), tracks them down and adds a relevant import statement to the setup script.
    Clocker.setup-A list of variables to be defined in the setup script. Basically, for each key, creates a line "key=value". The value CAN be set to a normal value instead of a string containing it, but this only works in cases where __repr__ is a valid constructor (lists,dictionaries,and numbers, but not normal strings (they must be nested within additional '')). For best use, have your setup values all be strings.
    Clocker.runcount- Pretty self-explanatory, just the number of times for timeit to run.
    Clocker.make_script()-Exposed so that you can check and see that the script is there and working correctly. This is called automatically on Clocker.run().
    Clocker.run()-Basically just times setup script and then gets results for all tests, runcount times, through timeit.
    Attached Files
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    allforall forms the Cartesian product of its iterable of strings arguments. You've used it merely as a test program to demonstrate clocker. As a side note, allforall appears to work. I did
    Code:
    >>> len(allforall(['red','blue','green'],['cat','dog','monkey'],'ABCDE'))
    45
    == 3*3*5, and also browsed the output, which looked correct. (allforall() raises an exception rather than returning []. I do sometimes test edge cases. In other words, no arguments.)

    clocker is a front end to timeit which I'll try harder to understand next time I want to time variant algorithms to express a problem solution. Who knows, I might remember this and provide feedback. I'll copy the thread URL into the program.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    80
    Rep Power
    2
    Yes. For once, this wasn't asking for help. The forum is called "Python Programming", not "Python Help" or some such. :P
    I thought this thread might provide a refreshing pause from the usual torrent of questions.

    Yes, that is exactly what allforall does and was meant to do. I've wanted a program that does that for quite awhile. Couldn't think of anything clearer to name it. So I called it that as shortform for "all x for all y".

    Thank you for taking the time to look at it and post, Dave.

IMN logo majestic logo threadwatch logo seochat tools logo