Created
July 31, 2009 13:53
-
-
Save cdent/159233 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from time import time | |
| starts = [ | |
| 1248637121, | |
| 1248818005, | |
| 1248819043, | |
| 1248819661, | |
| 1248820029, | |
| 1248862668, | |
| 1248976328, | |
| 1248989370, | |
| 1249036736, | |
| ] | |
| stops = [ | |
| 1248817256, | |
| 1248817974, | |
| 1248819674, | |
| 1248819680, | |
| 1248819813, | |
| 1248819936, | |
| 1248819977, | |
| 1248820035, | |
| 1248820188, | |
| 1248873042, | |
| 1248978031, | |
| 1248992420, | |
| 1249036746, | |
| ] | |
| def zip_to_type(starts, stops): | |
| tuples = [] | |
| for start in starts: | |
| tuples.append(('start', start)) | |
| for stop in stops: | |
| tuples.append(('stop', stop)) | |
| def key_gen(tuple): | |
| return tuple[1] | |
| tuples = sorted(tuples, key=key_gen) | |
| return tuples | |
| def flush_until_different(type, tuples): | |
| removals = [] | |
| for tuple in tuples: | |
| if tuple[0] == type: | |
| removals.append(tuple) | |
| else: | |
| break | |
| return removals | |
| def de_gap(type, tuples): | |
| if type == 'stop': | |
| tuples.reverse() | |
| index = 0 | |
| removals = [] | |
| for tuple in tuples: | |
| if tuple[0] == type: | |
| removals.extend(flush_until_different(type, tuples[index + 1:])) | |
| index += 1 | |
| for removeme in removals: | |
| try: | |
| tuples.remove(removeme) | |
| except ValueError: | |
| pass | |
| if type == 'stop': | |
| tuples.reverse() | |
| return tuples | |
| def totaller(tuples): | |
| total = 0 | |
| while 1: | |
| try: | |
| try: | |
| start = tuples.pop(0)[1] | |
| except IndexError: | |
| raise | |
| try: | |
| stop = tuples.pop(0)[1] | |
| except IndexError: | |
| stop = time() | |
| except IndexError: | |
| return total | |
| total += (stop - start) | |
| def count_times(starts, stops): | |
| return totaller( | |
| de_gap('stop', | |
| de_gap('start', | |
| zip_to_type(starts, stops)))) | |
| if __name__ == '__main__': | |
| print "with stopper" | |
| seconds = count_times(starts, stops) | |
| print float(seconds)/(60*60), 'hours' | |
| print "without stopper" | |
| stops.pop() | |
| seconds = count_times(starts, stops) | |
| print float(seconds)/(60*60), 'hours' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment